Easy processing of XML data in the. NET Framework (1-3)

Source: Internet
Author: User
?? The type of each node is one of the XmlNodeType enumerations, and in the code shown in three, we use only two of these types: Element and EndElement. The output source re-customizes the original document structure, which discards or neglects the attributes and node contents of the XML element, and outputs only the element node name. Let's say we have the following XML fragment applied:

?? <mags>
?? <mag name= ' MSDN Magazine ' >
?? MSDN Magazine
?? </mag>
?? <mag name= ' MSDN Voices ' >
?? MSDN Voices
?? </mag>
?? </mags>
?? The results from the above program output are as follows:

?? <mags>
?? <mag>
?? </mag>
?? <mag>
?? </mag>
?? </mags>

?? The indent for a child node is set according to the browser's depth attribute (depth property), and the depth property returns a shaped data that shows the nesting level of the current node. All text is placed in the StringWriter object (a very handy stream-based class that encapsulates the Strigbuilder class).

?? As mentioned earlier, the browser does not actively visit attribute nodes through the Read method. To visit the attribute node aggregation of the current element, it is necessary to traverse the aggregation with a brief loop that is controlled by the return value of the MoveToNextAttribute method. The following code is used to visit all the properties of the current node and combine the name of the property and its value with a comma to form a string:
?? if (reader. HasAttributes)
?? while (reader. MoveToNextAttribute ())
?? BUF = reader. Name ' =\ ' reader. Value ' \ ', ';
?? Reader. Movetoelement ();

?? When you are finished processing the property set, call the Movetoelement method to return the pointer to the element node to which the property belongs. To be exact, the Movetoelement method is not a real move pointer, because the pointer never moves away from the element node when the property set is processed. The Movetoelement method simply points to an internal member and gets the value of the member in turn. For example, use the Name property to get the property name of a property, and then call the Movetoelement method to move the pointer to the element node to which it belongs. But you don't have to call the Movetoelement method when you don't need to keep working on the other nodes.


The above is the easy processing of XML data (1-3) in the. NET framework, and more about topic.alibabacloud.com (www.php.cn)!

  • 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.