XML data read performance comparison embodiment (a)

Original link: http://www.cnblogs.com/jvstudio/archive/2010/08/12/1797809.html

  For months, allegedly SOA, and XML has been in operation deal, SQL almost forgot to light up. It is now known that at least four common man XML data manipulation mode (almost like Java), but have not actually compare these methods have their pros and cons or what features. Just to see the Internet did not experiment in this area, even to summarize.

  Start by reading the XML source testing, with a relatively large RSS file link , copied to the project bin / debug directory.

 
   
Stream xmlStream = new MemoryStream(File.ReadAllBytes(path));

    

  A, XmlDocument way

ExpandedBlockStart.gif Code
 
    
1 static IList testXmlDocument()
2 {
3 var doc = new XmlDocument();
4 doc.Load(xmlStream);
5 var nodeList = doc.DocumentElement.ChildNodes;
6 var lstChannel = new List < Object > (nodeList.Count );
7 foreach (XmlNode node in nodeList)
8 {
9 var channel = new
10 {
11 Title = node.SelectSingleNode( " title " ).InnerText,
12 Link = node.SelectSingleNode( " link " ).InnerText,
13 Description = node.SelectSingleNode( " description " ).InnerText,
14 Content = node.SelectSingleNode( " content " ).InnerText,
15 PubDate = node.SelectSingleNode( " pubDate " ).InnerText,
16 Author = node.SelectSingleNode( " author " ).InnerText,
17 Category = node.SelectSingleNode( " category " ).InnerText
18 };
19 lstChannel.Add(channel);
20 }
21 return lstChannel;
22 }

 

  Two, XPathNavigator way

ExpandedBlockStart.gif Code
 
    
1 static IList testXmlNavigator()
2 {
3 var doc = new XmlDocument();
4 doc.Load(xmlStream);
5 var nav = doc.CreateNavigator();
6 nav.MoveToRoot();
7 var nodeList = nav.Select( " /channel/item " );
8 var lstChannel = new List < Object > (nodeList.Count);
9 foreach (XPathNavigator node in nodeList)
10 {
11 var channel = new
12 {
13 Title = node.SelectSingleNode( " title " ).Value,
14 Link = node.SelectSingleNode( " link " ).Value,
15 Description = node.SelectSingleNode( " description " ).Value,
16 Content = node.SelectSingleNode( " content " ).Value,
17 PubDate = node.SelectSingleNode( " pubDate " ).Value,
18 Author = node.SelectSingleNode( " author " ).Value,
19 Category = node.SelectSingleNode( " category " ).Value
20 };
21 lstChannel.Add(channel);
22 }
23 return lstChannel;
24 }

 

  Three, XmlTextReader way

ExpandedBlockStart.gif Code
 
    
1 static List < Channel > testXmlReader()
2 {
3 var lstChannel = new List < Channel > ();
4 var reader = XmlReader.Create(xmlStream);
5 while (reader.Read())
6 {
7 if (reader.Name == " item " && reader.NodeType == XmlNodeType.Element)
8 {
9 var channel = new Channel();
10 lstChannel.Add(channel);
11 while (reader.Read())
12 {
13 if (reader.Name == " item " ) break ;
14 if (reader.NodeType != XmlNodeType.Element) continue ;
15 switch (reader.Name)
16 {
17 case " title " :
18 channel.Title = reader.ReadString();
19 break ;
20 case " link " :
21 channel.Link = reader.ReadString();
22 break ;
23 case " description " :
24 channel.Description = reader.ReadString();
25 break ;
26 case " content " :
27 channel.Content = reader.ReadString();
28 break ;
29 case " pubDate " :
30 channel.PubDate = reader.ReadString();
31 break ;
32 case " author " :
33 channel.Author = reader.ReadString();
34 break ;
35 case " category " :
36 channel.Category = reader.ReadString();
37 break ;
38 default :
39 break ;
40 }
41 }
42 }
43 }
44 return lstChannel;
45 }

 

  Four, Linq to XML way

ExpandedBlockStart.gif Code
 
    
1 static IList testXmlLinq()
2 {
3 var xd = XDocument.Load(xmlStream);
4 var list = from node in xd.Elements( " channel " ).Descendants( " item " )
5 select new
6 {
7 Title = node.Element( " title " ).Value,
8 Link = node.Element( " link " ).Value,
9 Description = node.Element( " description " ).Value,
10 Content = node.Element( " content " ).Value,
11 PubDate = node.Element( " pubDate " ).Value,
12 Author = node.Element( " author " ).Value,
13 Category = node.Element( " category " ).Value
14 };
15 return list.ToList();
16 }

 

  Test Results:

XmlDocment 47ms
XPathNavigator 42ms
XmlTextReader 23ms
Xml Linq 28ms

 

  Summary of your own understanding, XmlDocument basic operation mode of operation according to the W3C DOM, but you want all the nodes to resolve objects are loaded into memory, often resulting in tremendous waste. So Microsoft's own programming specification is not recommended to use it. Because here to read all the nodes, and performance may therefore less the way Navigator. Of the three random read mode, Xml Linq highest performance, but the method name a little awkward. XmlTextReader way is called SAX, read-only forward, no doubt the highest performance, but a lot of trouble on the implementation, to be more precise access control logic, you can not use an anonymous class to store data.

  .Net 3.5 release Xml Linq can well replace the first two methods, usually the case, it is best to use it. Only a few occasions, if the performance is extremely demanding, or read Xml too much data at once can not be downloaded or read into memory, it had committed to XmlTextReader the pain.

 

Address reprint: http://www.cnblogs.com/XmNotes/archive/2010/08/12/1796162.html

Reproduced in: https: //www.cnblogs.com/jvstudio/archive/2010/08/12/1797809.html

Guess you like

Origin blog.csdn.net/weixin_30900589/article/details/94798289