Home > Article > Backend Development > Easily process XML data in .NET Framework (1-3)
??The type of each node is one of the XmlNodeType enumerations. In the code shown in Figure 3, we only use two of the types: Element and EndElement. The output source code re-customizes the original document structure. It discards or ignores the attributes and node content of the XML element, and only outputs the element node name. Suppose we apply the following XML fragment:
??<mags>
??<mag name='MSDN Magazine'>
??MSDN Magazine
??</mag>
??<mag name='MSDN Voices '>
??MSDN Voices
??</mag>
??</mags>
??The output results of using the above program are as follows:
??<mags>
??<mag>
??< /mag>
??<mag>
??</mag>
??</mags>
??The indentation amount of the child node is set according to the depth attribute (Depth attribute) of the browser, and the Depth attribute returns An integer representing the nesting level of the current node. All text is placed in a StringWriter object (a very convenient stream-based wrapper around the StrigBuilder class).
??As mentioned before, the browser will not automatically access the attribute node through the Read method. To access the current element's attribute node collection, you must use a simple loop controlled by the return value of the MoveToNextAttribute method to traverse the collection. The following code is used to access all attributes of the current node and combine the attribute name and its value into a string separated by commas:
??if (reader.HasAttributes)
??while(reader.MoveToNextAttribute())
??buf = reader.Name '='' reader.Value '',';
??reader.MoveToElement();
??When you finish processing the attribute set, call the MoveToElement method to return the pointer to The element node to which the attribute belongs. To be precise, the MoveToElement method does not really move the pointer, because the pointer never moves from the element node when processing the attribute set. The MoveToElement method just points to an internal member and obtains the value of the member in turn. For example, use the Name attribute to obtain the attribute name of an attribute, and then call the MoveToElement method to move the pointer to the element node to which it belongs. But when you don't need to continue processing other nodes, you don't need to call the MoveToElement method.
The above is the content of easily processing XML data (1-3) in .NET Framework. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!