Performance Comparison of XML data reading methods (1)

Source: Internet
Author: User
In the past few months, SOA has been suspected of dealing with xml operations, and SQL is almost forgotten. In the past few months, SOA has been suspected of dealing with xml operations, and SQL is almost forgotten. Now we know that there are at least four common methods of XML data operations (like java), but we have not actually compared the characteristics or advantages of these methods. I just saw that there was no such experiment on the internet. I would like to summarize it.

The test starts by reading the XML source and copying it to the bin/debug Directory of the project with a large rss File link.

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

   

I. XmlDocument method

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(NodeList. count); 7 foreach (XmlNode node in nodeList) 8 {9 var channel = new10 {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 "). innerText18}; 19 lstChannel. add (channel); 20} 21 return lstChannel; 22}

II. XPathNavigator mode

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(NodeList. count); 9 foreach (XPathNavigator node in nodeList) 10 {11 var channel = new12 {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 "). value20}; 21 lstChannel. add (channel); 22} 23 return lstChannel; 24}

III. XmlTextReader method

Code

1 static List
     
       testXmlReader() 2 { 3     var lstChannel = new List
      
       (); 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 }
      
     

IV. Using the Linq to XML method

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").Value14                 };15     return list.ToList();16 }

Test results:

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

As a summary, the operation of XmlDocument is basically performed by W3C DOM, But parsing all nodes into objects and loading them into memory often results in a great waste. Therefore, Microsoft's own programming specifications are not recommended. Because all nodes are read, the performance may be slightly different from that of Navigator. Among the three random reading methods, Xml Linq has the highest performance, but the method name is a bit awkward. The XmlTextReader method is the so-called SAX. the read-only forward method undoubtedly has the highest performance. However, it is a lot of trouble to implement it. to precisely control the access logic, you cannot use an anonymous class to store data.

The release of Xml Linq in. Net 3.5 can replace the first two methods. In general, it is best to use it. In some cases, if the performance requirement is extremely high, or the Xml data size is too large to be downloaded or read to the memory at once, the XmlTextReader is the only cause.

The above content compares the performance of the XML data reading method (1). For more information, see PHP Chinese network (www.php1.cn )!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.