XmlReader.ReadSubtree Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns a new XmlReader
instance that can be used to read the current node, and all its descendants.
public:
virtual System::Xml::XmlReader ^ ReadSubtree();
public virtual System.Xml.XmlReader ReadSubtree ();
abstract member ReadSubtree : unit -> System.Xml.XmlReader
override this.ReadSubtree : unit -> System.Xml.XmlReader
Public Overridable Function ReadSubtree () As XmlReader
A new XML reader instance set to Initial. Calling the Read() method positions the new reader on the node that was current before the call to the ReadSubtree() method.
The XML reader isn't positioned on an element when this method is called.
-or-
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."
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
reader.ReadToFollowing("Book");
reader.Skip();
// Create another reader that contains just the second book node.
XmlReader inner = reader.ReadSubtree();
inner.ReadToDescendant("Title");
Console.WriteLine(inner.Name);
// Do additional processing on the inner reader. After you
// are done, call Close on the inner reader and
// continue processing using the original reader.
inner.Close();
}
Dim settings As New XmlReaderSettings()
settings.IgnoreWhitespace = True
Using reader As XmlReader = XmlReader.Create("books.xml", settings)
' Position the reader on the second book node.
reader.ReadToFollowing("Book")
reader.Skip()
' Create another reader that contains just the second book node.
Dim inner As XmlReader = reader.ReadSubtree()
inner.ReadToDescendant("Title")
Console.WriteLine(inner.Name)
' Do additional processing on the inner reader. After you
' are done, call Close on the inner reader and
' continue processing using the original reader.
inner.Close()
End Using
Use the following XML data to run the examples in this topic:
<?xml version="1.0" encoding="utf-8" ?>
<Books>
<Book>
<Title>A Brief History of Time</Title>
</Book>
<Book>
<Title>Principle Of Relativity</Title>
</Book>
<Book>
<Title>Victory of Reason</Title>
</Book>
<Book>
<Title>The Unicorn that did not Fail</Title>
</Book>
<Book>
<Title>Rational Ontology</Title>
</Book>
<Book>
<Title>The Meaning of Pizza</Title>
</Book>
</Books>
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 reader is 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.
Note
The ReadSubtree method isn't intended for creating copies of the XML data that you can work with independently. It's designed to 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.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: