Read XML using xmlreader

Source: Internet
Author: User

When reading XML, xmldocument and xelement should put the entire XML file into the memory for operations. This operation is simple, but it is very memory-intensive.And I/O (may be disk I/O or network I/O)In some scenarios, we must consider saving as much memory as possible.And Io overheadIn this caseXmlreaderAnd 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.

Example of reading XML content as followsCodeAnd annotations

// 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 xmlreader. create parameter 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> ")) {// use xmlreader. create static method to create xmlreader instance using (xmlreader RDR = xmlreader. create (strrdr) {// cyclically read method until the end of the document while (RDR. read () {console. writeline ("RDR. nodetype = "+ RDR. nodetype); // if it is the Start Node if (RDR. nodetype = xmlnodetype. element) {// use RDR. obtain the node name string elementname = RDR. name; console. writeline (elementname + "element start"); If (elementname = "root") {}// reads the cat element at this time RDR. read () reads <cat color = "white"> else if (elementname = "cat ") {// you can obtain the attribute value string colorval = RDR ["color"] through brackets; console. writeline ("\ tcat's color is" + colorval); // read the text in the node if (RDR. read () {// use RDR. value to obtain the text content console. writeline ("\ t Cat said:" + RDR. value) ;}} else if (RDR. nodetype = xmlnodetype. endelement) {// you can use RDR when the node ends. name: Get the node name 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

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.