XmlTextReader is used for fast forward-only read XML.
The XmlTextReader namespace is:
System.Xml
application Example
The code is as follows |
Copy Code |
XmlTextReader reader = new XmlTextReader (xmlfragment, xmlnodetype.element, NULL); while (reader. Read ()) { if (reader. NodeType = = XmlNodeType.Element) { if (reader. Name = = "State") { state = (sendingshipstate) convert.toint32 (reader. ReadString ()); } else if (reader. Name = = "message") { Mymessage.analyzexml (reader. ReadInnerXml ()); } else if (reader. Name = = "Writer") { Writer = reader. ReadString (); } } } Reader. Close (); |
XmlTextReader has multiple constructors, parameters can use URLs (URLs or file paths), Stream, TextReader, XML strings, and so on, where XML strings are used.
XmlTextReader is not allowed to rewind, so use reader. Read () reads from the go, and then determines the type of node that is currently being read, note: Whitespace is also a node.
We then judge the label name based on the name attribute value, for example, the <root> Name property value is root.
The code is as follows |
Copy Code |
Mymessage.analyzexml (reader. ReadInnerXml ()); |
Here we give all of the XML content under the node to another object for processing.
Take attribute value
code is as follows |
copy code |
XmlTextReader reader = new XmlTextReader ("Data.xml"); While reader. Read ()) { if (reader). NodeType = = XmlNodeType.Element && reader. Name = = "category") { MessageBox.Show (reader). GetAttribute ("text")); } } Reader. Close (); |