.NET API Reference

Microsoft Docs

SourceXmlReader Property

Gets the underlying XmlReader that the XmlNodeReader reads from.

Syntax

public XmlReader SourceXmlReader { get; }

Return Value

Returns an XmlReader object that provides the XML data source for the XmlNodeReader. The returned reader is positioned at the start of the underlying XML document.

Remarks

The SourceXmlReader property is read‑only. It allows you to access the original XmlReader that was passed to the XmlNodeReader constructor or that was created internally when the XmlNodeReader was constructed from an XmlNode.

When you modify the state of the returned XmlReader, the changes are reflected in the XmlNodeReader because they share the same underlying stream.

Example

using System;
using System.Xml;

class Demo
{
    static void Main()
    {
        string xml = @"<root><child id='1'>Value</child></root>";
        using (XmlReader reader = XmlReader.Create(new System.IO.StringReader(xml)))
        using (XmlNodeReader nodeReader = new XmlNodeReader(reader))
        {
            Console.WriteLine("Source type: " + nodeReader.SourceXmlReader.GetType());
            // Output: Source type: System.Xml.XmlTextReaderImpl

            while (nodeReader.Read())
            {
                if (nodeReader.NodeType == XmlNodeType.Element && nodeReader.Name == "child")
                {
                    Console.WriteLine($"Element: {nodeReader.Name}, Value: {nodeReader.Value}");
                }
            }
        }
    }
}

See Also