search
HomeBackend DevelopmentXML/RSS TutorialPerformance 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 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
RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

Building Feeds with XML: A Hands-On Guide to RSSBuilding Feeds with XML: A Hands-On Guide to RSSApr 14, 2025 am 12:17 AM

The steps to build an RSSfeed using XML are as follows: 1. Create the root element and set the version; 2. Add the channel element and its basic information; 3. Add the entry element, including the title, link and description; 4. Convert the XML structure to a string and output it. With these steps, you can create a valid RSSfeed from scratch and enhance its functionality by adding additional elements such as release date and author information.

Creating RSS Documents: A Step-by-Step TutorialCreating RSS Documents: A Step-by-Step TutorialApr 13, 2025 am 12:10 AM

The steps to create an RSS document are as follows: 1. Write in XML format, with the root element, including the elements. 2. Add, etc. elements to describe channel information. 3. Add elements, each representing a content entry, including,,,,,,,,,,,. 4. Optionally add and elements to enrich the content. 5. Ensure the XML format is correct, use online tools to verify, optimize performance and keep content updated.

XML's Role in RSS: The Foundation of Syndicated ContentXML's Role in RSS: The Foundation of Syndicated ContentApr 12, 2025 am 12:17 AM

The core role of XML in RSS is to provide a standardized and flexible data format. 1. The structure and markup language characteristics of XML make it suitable for data exchange and storage. 2. RSS uses XML to create a standardized format to facilitate content sharing. 3. The application of XML in RSS includes elements that define feed content, such as title and release date. 4. Advantages include standardization and scalability, and challenges include document verbose and strict syntax requirements. 5. Best practices include validating XML validity, keeping it simple, using CDATA, and regularly updating.

From XML to Readable Content: Demystifying RSS FeedsFrom XML to Readable Content: Demystifying RSS FeedsApr 11, 2025 am 12:03 AM

RSSfeedsareXMLdocumentsusedforcontentaggregationanddistribution.Totransformthemintoreadablecontent:1)ParsetheXMLusinglibrarieslikefeedparserinPython.2)HandledifferentRSSversionsandpotentialparsingerrors.3)Transformthedataintouser-friendlyformatsliket

Is There an RSS Alternative Based on JSON?Is There an RSS Alternative Based on JSON?Apr 10, 2025 am 09:31 AM

JSONFeed is a JSON-based RSS alternative that has its advantages simplicity and ease of use. 1) JSONFeed uses JSON format, which is easy to generate and parse. 2) It supports dynamic generation and is suitable for modern web development. 3) Using JSONFeed can improve content management efficiency and user experience.

RSS Document Tools: Building, Validating, and Publishing FeedsRSS Document Tools: Building, Validating, and Publishing FeedsApr 09, 2025 am 12:10 AM

How to build, validate and publish RSSfeeds? 1. Build: Use Python scripts to generate RSSfeed, including title, link, description and release date. 2. Verification: Use FeedValidator.org or Python script to check whether RSSfeed complies with RSS2.0 standards. 3. Publish: Upload RSS files to the server, or use Flask to generate and publish RSSfeed dynamically. Through these steps, you can effectively manage and share content.

Securing Your XML/RSS Feeds: A Comprehensive Security ChecklistSecuring Your XML/RSS Feeds: A Comprehensive Security ChecklistApr 08, 2025 am 12:06 AM

Methods to ensure the security of XML/RSSfeeds include: 1. Data verification, 2. Encrypted transmission, 3. Access control, 4. Logs and monitoring. These measures protect the integrity and confidentiality of data through network security protocols, data encryption algorithms and access control mechanisms.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.