.NET API Documentation

XmlNodeReader Class

public ref class XmlNodeReader : public XmlReader

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

Methods

Constructors

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