asp.net XmlTextReader parse XML document instance

Source: Internet
Author: User

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 ();

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.