Performance comparison of XML data reading methods (1)
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 methodCode1 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 (m.sbmmt.com)! ###
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20443
7
13592
4
JSON vs. XML: Why RSS Chose XML
May 05, 2025 am 12:01 AM
RSS chose XML instead of JSON because: 1) XML's structure and verification capabilities are better than JSON, which is suitable for the needs of RSS complex data structures; 2) XML was supported extensively at that time; 3) Early versions of RSS were based on XML and have become a standard.
RSS in XML: Decoding Tags, Attributes, and Structure
Apr 24, 2025 am 12:09 AM
RSS is an XML-based format used to publish and subscribe to content. The XML structure of an RSS file includes a root element, an element, and multiple elements, each representing a content entry. Read and parse RSS files through XML parser, and users can subscribe and get the latest content.
Understanding RSS Documents: A Comprehensive Guide
May 09, 2025 am 12:15 AM
RSS documents are a simple subscription mechanism to publish content updates through XML files. 1. The RSS document structure consists of and elements and contains multiple elements. 2. Use RSS readers to subscribe to the channel and extract information by parsing XML. 3. Advanced usage includes filtering and sorting using the feedparser library. 4. Common errors include XML parsing and encoding issues. XML format and encoding need to be verified during debugging. 5. Performance optimization suggestions include cache RSS documents and asynchronous parsing.
XML's Advantages in RSS: A Technical Deep Dive
Apr 23, 2025 am 12:02 AM
XML has the advantages of structured data, scalability, cross-platform compatibility and parsing verification in RSS. 1) Structured data ensures consistency and reliability of content; 2) Scalability allows the addition of custom tags to suit content needs; 3) Cross-platform compatibility makes it work seamlessly on different devices; 4) Analytical and verification tools ensure the quality and integrity of the feed.
Building XML Applications with C : Practical Examples
May 03, 2025 am 12:16 AM
You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.
XML in C : Handling Complex Data Structures
May 02, 2025 am 12:04 AM
Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.
RSS, XML and the Modern Web: A Content Syndication Deep Dive
May 08, 2025 am 12:14 AM
RSS and XML are still important in the modern web. 1.RSS is used to publish and distribute content, and users can subscribe and get updates through the RSS reader. 2. XML is a markup language and supports data storage and exchange, and RSS files are based on XML.
Beyond Basics: Advanced RSS Features Enabled by XML
May 07, 2025 am 12:12 AM
RSS enables multimedia content embedding, conditional subscription, and performance and security optimization. 1) Embed multimedia content such as audio and video through tags. 2) Use XML namespace to implement conditional subscriptions, allowing subscribers to filter content based on specific conditions. 3) Optimize the performance and security of RSSFeed through CDATA section and XMLSchema to ensure stability and compliance with standards.




