C # operation XML: Using XmlReader to read XML

Source: Internet
Author: User

XmlDocument and XElement are going to have to put the entire XML document into memory when reading XML, which is simple, but very costly to memory and IO (possibly disk IO or network IO), while in some scenarios we have to consider saving as much memory as possible. and Io overhead , it's time for XmlReader and XmlWriter to play.

XmlReader read XML requires a read () instance method that reads the declaration in the XML document continuously, node start, node content, node end, and whitespace, and so on, until the end of the document, the Read () method returns FALSE.

Read the XML content instance code and comment description as follows

?
//玉开技术博客: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)        {            //声明StringReader传入Xml文本,作为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>"))            {                //通过XmlReader.Create静态方法创建XmlReader实例                using (XmlReader rdr = XmlReader.Create(strRdr))                {                    //循环Read方法直到文档结束                    while (rdr.Read())                    {                        Console.WriteLine("rdr.NodeType = " + rdr.NodeType);                        //如果是开始节点                        if (rdr.NodeType == XmlNodeType.Element) {                            //通过rdr.Name得到节点名                            string elementName = rdr.Name;                                                         Console.WriteLine(elementName + " element start");                            if (elementName == "root") {                             }                            //读取到cat元素 这时rdr.Read()读取到的内容为<cat color="white">                            else if (elementName == "cat")                            {                                //可以通过中括号获得属性值                                string colorVal = rdr["color"];                                Console.WriteLine("\tcat‘s color is " + colorVal);                                //读取到节点内文本内容                                if(rdr.Read()) {                                    //通过rdr.Value获得文本内容                                    Console.WriteLine("\t cat said:" + rdr.Value);                                }                            }                        }                        else if (rdr.NodeType == XmlNodeType.EndElement)                        {                            //在节点结束时也可以通过rdr.Name获得节点名字                            string elementName = rdr.Name;                            Console.WriteLine(elementName + " element end");                        }                    }                }            }            Console.Read();        }    }}
If you think the code is not clear, here is a reading sequence diagram, indicating the reading sequence number and the contents of each read, as shown in: You can see XmlReader reading this xml: The 1th Time Read () reads the XML document declaration part 2nd Reading () Read the blank after the declaration of the 3rd time read () reads the root node root of the Start tab the 4th time read () reads the root node after the beginning of the blank 5th read () reads the beginning of the Cat node, From the left angle bracket to the right angle bracket including the properties of the node the 6th time the contents of the Cat node are read the 7th read is the end tag of the Cat node 8th reads the white space after the cat node end tag is read the beginning of the dog node, Note that the 10th read from the left angle bracket to the end slash is the end of the dog label/> The 11th read is the blank after the end of the dog tag, and the 12th reads the end tag of the root.

Thanks to @kingthy for the problem of consuming IO in the text, XmlReader and XmlDocument consume the same IO; the difference is that XmlReader can read a little, show a bit, and XmlDocument must be fully read before you can start processing.

C # Related essays on XML processing:

1. Read and write XML documents via XmlDocument 2. Use XmlReader to read XML and write XML3 using XmlWriter. Using LINQ to XML Access XML4. Defines a fixed-format XML document 5.XML serialization or deserialization by Xmlscheme 6. Finding XML nodes through XPath 7. Transforming XML Formats through XSLT

C # operation XML: Using XmlReader to read XML

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.