Collapse the table of content
Expand the table of content
Expand Minimize

XmlReader.ReadSubtree Method

Returns a new XmlReader instance that can be used to read the current node, and all its descendants.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public virtual XmlReader ReadSubtree()

Return Value

Type: System.Xml.XmlReader
A new XmlReader instance set to ReadState.Initial. A call to the Read method positions the new XmlReader on the node that was current before the call to ReadSubtree method.


The XmlReader is not positioned on an element when this method is called.

ReadSubtree can be called only on element nodes. When the entire sub-tree has been read, calls to the Read method returns false. When the new XmlReader has been closed, the original XmlReader will be positioned on the EndElement node of the sub-tree. Thus, if you called the ReadSubtree method on the start tag of the book element, after the sub-tree has been read and the new XmlReader has been closed, the original XmlReader is positioned on the end tag of the book element.

You should not perform any operations on the original XmlReader until the new XmlReader has been closed. This action is not supported and can result in unpredictable behavior.


The ReadSubtree method is not intended to create a copy of the XML data that you can work with independently. Rather, it can be used create a boundary around an XML element. This is useful if you need to pass data to another component for processing and you wish to limit how much of your data the component can access. When you pass an XmlReader returned by the ReadSubtree method to another application, the application can access only that XML element, rather than the entire XML document.

The following example shows how to use the ReadSubtree method.

XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
using (XmlReader reader = XmlReader.Create("books.xml", settings)) {

  // Position the reader on the second book node

  // Create another reader that contains just the second book node.
  XmlReader inner = reader.ReadSubtree();

  // Do additional processing on the inner reader. After you  
  // are done, call Close on the inner reader and  
  // continue processing using the original reader.


Use the following Xml data to run the examples in this topic.

<?xml version="1.0" encoding="utf-8" ?>
    <Title>A Brief History of Time</Title>
    <Title>Principle Of Relativity</Title>
    <Title>Victory of Reason</Title>
    <Title>The Unicorn that did not Fail</Title>
    <Title>Rational Ontology</Title>
    <Title>The Meaning of Pizza</Title>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft