Home  >  Article  >  Backend Development  >  Performance comparison of XML data reading methods (1)

Performance comparison of XML data reading methods (1)

黄舟
黄舟Original
2017-02-13 15:43:581531browse

In the past few months, I have been dealing with XML operations due to SOA, and I have almost forgotten all about SQL. It is now known that there are at least four commonly used XML data manipulation methods (similar to Java), but there is no actual comparison of the characteristics or advantages and disadvantages of these methods. I happened to see that there are no experiments in this area on the Internet, so I will summarize it.

At the beginning of the test, first read the XML source, use a relatively large rss file link, and copy it to the project bin/debug directory.

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

  

      

                 

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 }

           

                

                

##                       

                   

## 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 }

3. XmlTextReader method

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 }

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

Test results: ###
XmlDocment    47ms    
XPathNavigator    42ms    
XmlTextReader    23ms    
Xml Linq    28ms
### ###### To summarize my understanding, the operation of XmlDocument basically follows the W3C DOM operation method, but it needs to be All nodes are parsed into objects and loaded into memory, which often causes a lot of waste. Therefore, Microsoft's own programming standards do not recommend its use. Since all nodes are read here, the performance may not be much different from the Navigator method. 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, which is read-only and has the highest performance. However, it is a lot of trouble to implement. The access logic needs to be controlled more accurately, and anonymous classes cannot be used to store data. ###### .Net 3.5 Release Xml Linq can replace the first two methods very well. Under normal circumstances, it is best to use it. Only in some cases, if the performance requirements are extremely high, or the amount of Xml data to be read is too large to be downloaded or read into the memory at once, then you have to commit yourself to XmlTextReader. ######The above is the performance comparison of XML data reading methods (1). For more related content, please pay attention to the PHP Chinese website (www.php.cn)! ###
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn