XmlNodeReader Class
Represents a reader that provides fast, forward-only access to a node set that has been loaded into an XmlDocument
object. This class inherits from XmlReader
.
XmlNodeReader
implements the XmlReader
interface. It reads from an existing DOM node and traverses the node tree. This class is useful for passing an XmlDocument
object to an API that expects an XmlReader
.
Remarks
XmlNodeReader
reads the DOM tree and presents it as a stream of XML events. It does not process the document in any way, nor does it validate it. For example, it does not expand entity references or merge adjacent text nodes.
To create an XmlNodeReader
, you first create an XmlDocument
, load XML into it, and then create an XmlNodeReader
from a node within that document.
Properties
- AttributeCount Gets the number of attributes on the current node.
- BaseURI Gets the base URI of the current node.
-
CanResolveEntity
Gets a value indicating whether the
XmlReader
can process XSLT transform style-sheet instructions. - Depth Gets the depth of the current node in the XML document.
- EOF Gets a value indicating whether the reader is at the end of the stream.
- HasAttributes Gets a value indicating whether the current node has attributes.
- HasValue Gets a value indicating whether the current node has a simple value.
- IsDefault Gets a value indicating whether the value of the current node is typed as a default value.
- IsEmptyElement Gets a value indicating whether the current node is an empty element tag (for example, <tag/>).
- LocalName Gets the local name of the current node.
- Name Gets the qualified name of the current node.
- NamespaceURI Gets the namespace URI (as defined in World Wide Web Consortium) of the node on which the reader is positioned.
- NodeType Gets the type of the current node.
- Prefix Gets the namespace prefix of the current node.
- QuoteChar Gets the character used to quote attribute values.
- ReadState Gets the state of the reader.
- Value Gets the text value of the current node.
-
XmlLang
Gets the current
xml:lang
attribute value. -
XmlSpace
Gets the current
xml:space
attribute value.
Methods
- ReadAttributeValue Reads the text of an attribute.
- MoveToAttribute Moves to the attribute with the specified name.
- MoveToElement Moves to the element that contains the current attribute.
- MoveToFirstAttribute Moves to the first attribute of the current node.
- MoveToNextAttribute Moves to the next attribute of the current node.
- Read Moves the XmlReader to the next node.
- ReadContentAsBase64 Reads the content of the current node as Base64 encoded data.
- ReadContentAsBinHex Reads the content of the current node as BinHex encoded data.
- ReadContentAsString Reads the text content of the current node.
- ReadElementContentAsBase64 Reads the content of a text-only element as Base64 encoded data.
- ReadElementContentAsBinHex Reads the content of a text-only element as BinHex encoded data.
- ReadElementContentAsString Reads the content of a text-only element as a String.
- ReadInnerXml Reads all the markup representing the children of the current node.
- ReadOuterXml Reads the markup representing the current node and all its children.
- ReadString Reads the node value.
- ResolveEntity Resolves the entity reference.
- Skip Skips the current node, including all children.
Constructors
-
XmlNodeReader(XmlNode)
Initializes a new instance of the
XmlNodeReader
class with the specifiedXmlNode
.
Examples
The following example shows how to create an XmlNodeReader
from an XmlDocument
and use it to read XML data.
using System;
using System.Xml;
public class Example
{
public static void Main(string[] args)
{
// Create an XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<bookstore><book genre=\"cookbook\"><title lang=\"en\">Everyday Italian</title></book></bookstore>");
// Get the XmlNode to start reading from.
XmlNode node = doc.SelectSingleNode("//book");
// Create an XmlNodeReader.
using (XmlReader reader = new XmlNodeReader(node))
{
// Read and display the XML.
while (reader.Read())
{
// Print the node type, tag name, and value.
Console.Write($"{reader.NodeType}\t");
if (reader.Name != "")
{
Console.Write($"{reader.Name}\t");
}
if (reader.HasValue)
{
Console.Write($"{reader.Value}\t");
}
Console.WriteLine();
}
}
}
}