Use XmlReader to read Xml and xmlreader to read xml

Source: Internet
Author: User

Use XmlReader to read Xml and xmlreader to read xml
Read Xml using XmlReader

When reading Xml, XmlDocument and XElement should put the entire Xml file into the memory. This operation is simple, but memory and IO (may be disk IO or network IO) are expensive ); in some scenarios, we must consider saving the memory and IO overhead as much as possible.XmlReaderAnd XmlWriter.

XmlReader needs to Read the declaration, node start, node content, node end, and blank in the Xml document through the Read () instance method until the document ends, Read () method returns false.

The following example code and comments for reading Xml content

// Yukai technology blog: http://www.cnblogs.com/yukaizhaousing System;using System.Collections.Generic;using System.Text;using System.Xml;using System.IO; namespace UseXmlReader{    class Program    {        static void Main(string[] args)        {            // Declare StringReader to input Xml text as a parameter of XmlReader. Create            using (StringReader strRdr = new StringReader(@"<?xml version=""1.0"" encoding=""utf-8"" ?><root>    <cat color=""white"">I'm a Cat</cat>    <dog color=""yellow""/></root>"))            {                // Create an XmlReader instance using the XmlReader. Create static method                using (XmlReader rdr = XmlReader.Create(strRdr))                {                    // Cyclically Read until the end of the document                    while (rdr.Read())                    {                        Console.WriteLine("rdr.NodeType = " + rdr.NodeType);                        // If it is a Start Node                        if (rdr.NodeType == XmlNodeType.Element) {                            // Obtain the node Name through rdr. Name                            string elementName = rdr.Name;                                                         Console.WriteLine(elementName + " element start");                            if (elementName == "root") {                             }                            // When the cat element is Read, the content Read by rdr. Read () is <cat color = "white">                            else if (elementName == "cat")                            {                                // The attribute value can be obtained through brackets                                string colorVal = rdr["color"];                                Console.WriteLine("\tcat's color is " + colorVal);                                 // Read the text in the node                                if(rdr.Read()) {                                    // Obtain text content through rdr. Value                                    Console.WriteLine("\t cat said:" + rdr.Value);                                }                            }                        }                        else if (rdr.NodeType == XmlNodeType.EndElement)                        {                            // The node Name can also be obtained through rdr. Name at the end of the node                            string elementName = rdr.Name;                            Console.WriteLine(elementName + " element end");                        }                    }                }            }             Console.Read();        }    }}
If you do not understand the code, the following figure shows the Read sequence number and the content to be Read each time, as shown in: XmlReader reads the Xml file 1st times () read the declaration part of the Xml document 2nd Read () Read is the declared blank 3rd Read () Read is the root node root start label 4th Read () read the blank space after the root node starts 5th Read () reads the starting part of the cat node, from left angle brackets to right angle brackets, the attributes of the node are read 6th times. The content of the cat node is read 7th times. The ending label of the cat node is read 8th times. The cat node is read times. the white space after the tag is read for 9th times as the start part of the dog node, note that the dog tag is read for 10th times from the beginning of the Left angle bracket to before the ending slash/> 11th reads the white space after the dog tag is read for 12th reads the root tag end tag

Thanks @ Kingthy for asking a question about IO consumption in the article. XmlReader and XmlDocument consume the same IO. The difference is that XmlReader can read a little bit and display a little bit, xmlDocument can be processed only after it is fully read.

C # processing Xml:

1. read and Write Xml documents through XmlDocument 2. use XmlReader to read Xml, use XmlWriter to write Xml3. use Linq to xml to access XML4. use XmlScheme to define fixed-format xml documents. 5. xml serialization or deserialization Class 6. search for Xml nodes using XPath 7. convert Xml format using Xslt

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.