XmlReader.ReadSubtree XmlReader.ReadSubtree XmlReader.ReadSubtree XmlReader.ReadSubtree Method

定义

返回新的 XmlReader 实例,此实例可用于读取当前节点及其所有子节点。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

返回

设置为 Initial 的新 XML 实例。A new XML reader instance set to Initial. 调用 Read() 方法时,会将新的读取器定位在调用 ReadSubtree() 方法之前的当前节点上。Calling the Read() method positions the new reader on the node that was current before the call to the ReadSubtree() method.

异常

调用此方法时 XML 读取器没有定位在某个元素上。The XML reader isn't positioned on an element when this method is called.

- 或 --or-

在上一次异步操作完成之前调用了 XmlReader 方法。An XmlReader method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

示例

下面的示例显示如何使用 ReadSubtree 方法。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

使用以下 XML 数据来运行本主题中的示例: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 只能在元素节点上调用。ReadSubtree can be called only on element nodes. 读取整个子树后,调用Read方法将返回falseWhen the entire sub-tree has been read, calls to the Read method returns false. 当新的 XML 读取器已关闭时,原始读取器定位在EndElement的子树的节点。When the new XML reader has been closed, the original reader is positioned on the EndElement node of the sub-tree. 因此,如果您调用ReadSubtree未关闭的书元素后读取和新的 XML 读取器子树, 的开始标记上的方法,原始的 XML 读取器定位在的书元素的结束标记。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.

备注

ReadSubtree方法不用于创建可以独立地使用 XML 数据的副本。The ReadSubtree method isn't intended for creating copies of the XML data that you can work with independently. 它旨在创建 XML 元素周围的边界。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. 当传递的 XML 读取器返回的ReadSubtree到另一个应用程序,该应用程序的方法可以访问仅该 XML 元素,不是整个 XML 文档。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.

适用于