Comparison between XMLTextReader and XmlDocument for reading XML files

Source: Internet
Author: User
I saw an article on the Internet and tried it myself. As a result, XMLTextReader is faster!

The XMLTextReader class contained in the System. XML namespace Of the. NET Framework can quickly read data from XML files without high requirements on System resources. The XMLTextReader class can be used to read data from an XML file and convert it to HTML format for output in a browser.

 

Before reading this article, you need to know some basic knowledge: XML, HTML, C # programming language, and some knowledge about. NET, especially ASP. NET frameworks.

Microsoft's. NET Framework provides developers with a lot of development convenience. With the increasing importance of XML, developers are looking forward to developing a complete set of powerful XML tools .. The. NET Framework has failed to meet our expectations. In the System. XML namespace, the following classes are organized for XML:

XMLTextReader ------ provides quick, unidirectional, and unbuffered access to XML data. (One-way reading means you can only read XML files from the past, but not reverse reading)

XMLValidatingReader ------ used with the XMLTextReader class to verify the DTD, XDR, and XSD architectures.

XMLDocument ------ follows the level 1 and level 2 standards of W3C Document Object Model specifications to achieve random and cache access to XML data. The level-1 level includes the most basic part of the DOM, while the level-2 level has added a variety of improvements, including added support for namespace and level-2 link charts (css.

XMLTextWriter ------ generate XML files that comply with W3C XML 1.0 specifications.

This article mainly describes the first class XMLTextReader, which is designed to quickly read data from XML files, and for system resources (including memory and processor time) do not have high requirements. Under the control of the parent program, it gradually operates the XML file by processing only one node at a time to implement this kind of work process. In each node of the XML file, the parent program determines the node type, its attributes and data (if any), and other information about the node. Based on this information, the parent program can choose whether to process the node or ignore the node information to meet the needs of various application requests. This is called a pull processing model, because the parent program sends a request and extracts nodes from the XML file, and then processes it as needed or does not process it.
We can compare the XMLTextReader class with the simple XML application interface, that is, the SAX, which is another popular technology for programmers to read XML data. XMLTextReader and SAX are similar in that they can quickly read data from XML files without occupying a lot of system resources. However, unlike XMLTextReader's extract model, SAX uses a push-in model: the XML processor uses an "Event" to tell the Host application which node data is accessible and which node data is not; the Host Program responds or ignores the requests as needed. In other words, the data is transferred to the host from the SAX processing program. Programmers are bound to argue over the question of who is more advantageous in the pull and push-in processing models, but it is undeniable that both models can work well .. The. NET Framework does not support SAX, but you can use existing SAX tools, such as MSXML analyzer, For Your. NET program.

The XMLTextReader class has some constructors to adapt to various situations, such as reading data from an existing data stream or a unified resource location URL. Most commonly, if you want to read XML data from a file, a constructor will serve this purpose. Here is an example (all of my code examples use the C # language. If you prefer the visual basic language, it is easy to convert them ).

XMLTextReader myReader;
MyReader = New XMLTextReader ("c: \ data \ sales. XML ")

Create a loop called the Read () method. The return value of this method is always true until it reaches the bottom of the file. In other words, the loop starts at the beginning of the file and reads all the nodes. One node is read at a time until the end of the file is reached:

While (myReader. Read ()){
...
// Process each node here.
...
}

After each successful Read () call, the XMLTextReader instantiation program contains the information of the current node (that is, the node just Read from the file. We can obtain the above information from the XMLTextReader member, as described in table 1. We can use the NodeType attribute to determine the type of the current node. Based on the node type, the program code can read node data and check whether it has attributes. Whether to ignore it or perform corresponding operations and Processing Based on program needs.

When using the NodeType attribute, it is very important to understand how nodes relate to XML units. For example, you can view the following XML elements:

<City> Chongqing </city>

XMLtextReader regards this element as three nodes in the following order:

1. <city> the tag is read as the XMLNodeType. Element Node. The Element Name "city" can be obtained from the Name attribute of XMLTextReader.

2. The Text data "Chongqing" is read as a node of the XMLNodeType. Text type. The data "Chongqing" can be obtained from the Value attribute of XMLTextReader.

3. </city> labels are read as XMLNodeType. EndElement nodes. Similarly, the element Name "city" can be obtained from the Name attribute of XMLTextReader.

These are three important node types. Other types are described in. NET instructions. For more information, see related documents.

If XMLTextReader encounters an error, such as XML syntax violation, it throws a System. XML. XMLException exception. The code using this class should always be protected (in Try ...... Catch Block), as you will see in the demo program later.
This article is just a very simple article about the XMLTextReader class. The XMLTextReader class has a considerable number of members and cannot be described here. XMLTextReader provides considerable flexibility when reading XML data. Even so, I have made a lot of discussions to ensure that readers can compile programs to implement tasks that are often required in the real world, that is, read data from an XML file and output the data in HTML format to display the data in the browser.

This ASP. NET Program (SCRIPT) runs on the server and generates an HTML page to return to the browser. This script is provided in code segment 1, and the XML data file used for work is provided in code segment 2. You can see that the XML file contains a list of links. The purpose of the program is to display the list. To make it easier for us to observe, these lists have been formatted.

Run the program:

1. Save code segment 1 as the XMLTextReader. ASPx file, and code segment 2 as the XMLData. XML file.

2. Place the two files in a virtual folder of the network server that has installed the. NET Framework.

3. Open Internet Explorer and browse this ASPx file. For example, on a LAN server, the URL will be http: // localhost/xmltextreader. ASPx ;.

Most of the program work is done by the XMLDisplay class, especially by the ProcessXML () method. It reads XML data from a node each time. For the element of interest, the node data is written into the output result together with the node name followed by the colon and the corresponding HTML format tag. At this stage, the "output result" consists of a StringBuilder object in which HTML text is temporarily stored.

The ProcessXML () method is called from the LoadDocument () method. The task executed in this method is to generate an XMLTextReader instantiation program and load the XML file before calling ProcessXML. It also handles exceptions, and then generates error information and displays it in the browser. Finally, this method returns a string that contains either the generated HTML content or the error message if an exception occurs ,.

The program execution starts with the Page_Load () program. This step is automatically executed when the browser requests to browse this page. The code here instantiates the XMLDisplay class and calls its LoadDocument () method. If everything runs normally, formatted HTML-format return values will be copied to a <div> tag on the page, and the generated HTML document will be sent back to the browser and displayed.

Other. NET Framework classes, for example, how does the XMLDocument class read XML data? Unlike the XMLTextReader class, the XMLDocument class creates a node tree for the entire XML document in the memory. In this way, the XML data can be obtained randomly (in contrast to the linear method in which the XMLTextReader class obtains data), and the XML file data and structure can be modified with perfect flexibility. In addition, XMLDocument allows the execution of XSLT transformations. However, these additional features are at the cost of reduced running speed and more occupation of system resources.
Code snippet 1: XmlTextReader. aspx

<% @ Import Namespace = "System. Xml" %>

<Script language = "C #" runat = server>

Public class XmlDisplay
File: // This class reads and processes XML files.
{

Public string LoadDocument (String XmlFileName ){
XmlTextReader xmlReader = null;
StringBuilder html = new StringBuilder ();
Try {
File: // create an XMLTextReader instance.
XmlReader = new XmlTextReader (XmlFileName );
// Process XML files
Html. Append (ProcessXml (xmlReader ));
}
Catch (XmlException ex ){
Html. Append ("an XML exception occurred:" +
Ex. ToString ());
}
Catch (Exception ex ){
Html. Append ("a common exception occurred:" +
Ex. ToString ());
}
Finally
{
If (xmlReader! = Null)
XmlReader. Close ();
}
Return html. ToString ();
}

Private string ProcessXml (XmlTextReader xmlReader)
{
StringBuilder temp = new StringBuilder ();

File: // This method reads the XML file and generates the output HTML document.
While (xmlReader. Read ())
{
// Process the start point of an element node.
If (xmlReader. NodeType = XmlNodeType. Element)
{
File: // ignore <people> and <person> Elements
If (xmlReader. Name! = "Person") & (xmlReader. Name! = "People "))
{
File: // if it is a <category> element, start a new paragraph.
If (xmlReader. Name = "category ")
Temp. Append ("<p> ");
File: // Add the element name to the output
Temp. Append (xmlReader. Name + ":");
}
}
// Process text nodes
Else if (xmlReader. NodeType = XmlNodeType. Text)
Temp. Append (xmlReader. Value + "<br> ");
File: // process the end of an element node
Else if (xmlReader. NodeType = XmlNodeType. EndElement)
{
File: // if it is a <email> node, add the end section mark
If (xmlReader. Name = "email ")
Temp. Append ("</p> ");
}
} // End the while LOOP

Return temp. ToString ();

} File: // end the ProcessXML Method

} File: // end the XmlDisplay class

Private void Page_Load (Object sender, EventArgs e ){
File: // create an XmlDisplay instance
XmlDisplay XmlDisplayDemo = new XmlDisplay ();
Output. InnerHtml = XmlDisplayDemo. LoadDocument (Server. MapPath ("XMLData. xml "));
}
</Script>
<Html>
<Head>
</Head>
<Body>
<H2> Demonstrate the XmlTextReader class <Div id = "output" runat = "server"/>
</Body>
</Html>

 

 

 

1 static void Main (string [] args)
2 {
3 DateTime d1 = DateTime. Now;
4 XmlDocumentTest ();
5 DateTime d2 = DateTime. Now;
6 TimeSpan ts = d2-d1;
7
8 Console. WriteLine (ts. TotalMilliseconds );
9 Console. Read ();
10
11}
12
13
14 public static string XmlFileName = ".../../XML/1.xml ";
15
16 private static void XmlTextReaderTest ()
17 {
18 XmlTextReader reader = new XmlTextReader (XmlFileName );
19 while (reader. Read ())
20 {
21 bool exit = false;
22 switch (reader. NodeType)
23 {
24 case XmlNodeType. Element:
25 break;
26 case XmlNodeType. Text:
27 if (reader. Value = "last ")
28 {
29 exit = true;
30}
31 break;
32 case XmlNodeType. EndElement:
33 break;
34 default:
35 break;
36}
37 if (exit)
38 {
39 return;
40
41}
42
43}
44}
45
46 private static void XmlDocumentTest ()
47 {
48 XmlDocument xd = new XmlDocument ();
49 xd. Load (XmlFileName );
50 XmlNode node = xd. SelectSingleNode ("/people/person [category = 'last']");
51 Console. Write (node. Name );
52}

 

The result shows the first time consumed:
The second time is found:

 

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.