XmlReader.ReadSubtree Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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 XML reader instance set to ReadState.Initial. Calling the Read method positions the new reader on the node that was current before the call to the ReadSubtree method.

Exception Condition

The XML reader isn't positioned on an element when this method is called.


An XmlReader method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message “An asynchronous operation is already in progress.”

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 XML reader has been closed, the original readeris 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 XML reader has been closed, the original XML reader is positioned on the end tag of the book element.

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


The ReadSubtree method isn't intended for creatingcopies of the XML data that you can work with independently. It's designedto create a boundary around an XML element. This is useful if you want 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 XML reader returned by the ReadSubtree method to another application, the application can access only that XML element, not 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>

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top