Represents a reader that provides a sequential token view of an XML input stream.
Object
XmlReader
[Visual Basic] MustInherit Public Class XmlReader [C#] public abstract class XmlReader [C++] public __gc __abstract class XmlReader [JScript] public abstract class XmlReader
This class is an abstract base class. The following specific implementations of XmlReader are available:
Class | Remarks |
---|---|
XmlTextReader | Parses the contents of a string, a Stream, or a TextReader into a sequence of XML tokens. |
XmlNodeReader | Performs an in-order traversal of a tree of XmlNode instances, returning the appropriate XML token for each node. |
At the most fundamental level, applications can use the XmlTextReader class to convert text into a sequence of XML tokens. If an application needs to examine and/or create XML in a random access fashion (i.e. when the strictly sequential view of the XML input stream provided by an XmlTextReader is not sufficient), the ReadDocument and ReadNode methods can be used to read the XML input into a tree of XmlNode instances. Once in tree form, XML nodes can then be looked up by name, added, modified, or deleted.
An XmlNode tree may itself be viewed as a sequence of XML tokens. This is the purpose of the XmlNodeReader class. Given a root XmlNode, the XmlNodeReader performs an in-order traversal of the XML node tree, in effect presenting the same sequence of XML tokens as reading the text representation of the node tree.
When an XmlTextReader is given the following input text:
successive calls to the Read method reports the following sequence of nodes:
NodeType | ID | Text | Attr.ID | Attr.Value |
---|---|---|---|---|
Header | ("?xml", "") | null | ||
Text | ("#text", "") | "\r\n" | ||
StartTag | ("envelope", "") | null | ||
Text | ("#text", "") | "\r\n\t" | ||
StartTag | ("info", "") | null | ("count", "") | "123" |
EndTag | null | null | ||
Text | ("#text", "") | "\r\n\t" | ||
StartTag | ("order", "urn:acme.order.v1") | null | ("xmlns", "") | "urn:acme.order.v1" |
("xmlns:xsi", "") | "urn:xml-schema" | |||
("date", "") | "2000-04-19" | |||
Text | ("#text", "") | "\r\n\t\t" | ||
StartTag | ("shipTo", "urn:acme.order.v1") | null | ||
Text | ("#text", "") | "John Doe" | ||
EndTag | null | null | ||
Text | ("#text", "") | "\r\n\t\t" | ||
StartTag | ("item", "urn:acme.order.v1") | null | ("id", "") | "<123-44860-XYZ>" |
Text | ("#text", "") | "Lawnmower" | ||
EndTag | null | null | ||
Text | ("#text", "") | "\r\n\t\t" | ||
StartTag | ("price", "urn:acme.order.v1") | null | ("type", "urn:xml-schema") | "decimal" |
Text | ("#text", "") | "249.95" | ||
EndTag | null | null | ||
Text | ("#text", "") | "\r\n\t" | ||
EndTag | null | null | ||
Text | ("#text", "") | "\r\n\t" | ||
StartTag | ("bar", "urn:acme.foo") | null | ("xmlns:foo", "") | "urn:acme.foo" |
CData | ("#comment", "") | " fill this in " | ||
EndTag | null | null | ||
Text | ("#text", "") | "\r\n" | ||
EndTag | null | null | ||
Text | ("#text", "") | "\r\n" | ||
EOF | null | null |
In the table above, the syntax
Some notable behaviors are:
An XML reader is initially positioned at the beginning of the input stream, and before the first call to
Read or another method that consumes input, the NodeType property has the value BOF.
When the reader is positioned on the XML header, the attributes of the header can be examined through the Header property.
Whitespace between tags is reported as text nodes. The IsWhitespace property can be used to determine if a text node consists of whitespace characters only.
An empty tag is reported as a start tag immediately followed by an end tag. In other words, the input
When a start tag has multiple attributes (such as the
An
Default namespace declarations do not apply to attributes. The identifier of an attribute with no prefix (such as the
When an element or attribute is specified using a qualified name of the form
The identifier of an attribute that declares a namespace prefix (such as the
Entity references (such as
The
ID property is always null when the reader is positioned on an end tag or at the end of the input stream. For all other node types, the ID property is never null.
Namespace: System.Xml
Assembly: System.dll