XmlReader.ReadSubtree メソッド

定義

現在のノードとそのすべての子孫の読み取りに使用できる新しい XmlReader インスタンスを返します。

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 リーダー インスタンス。 Read() メソッドを呼び出すと、ReadSubtree() メソッドの呼び出しの前に、現在のノードに新しいリーダーが配置されます。

例外

このメソッドが呼び出されたとき、XML リーダーは要素に配置されません。

-または-

前の非同期操作が完了する前に、 XmlReader メソッドが呼び出されました。 この場合、"非同期操作は既に進行中です" というメッセージで InvalidOperationException がスローされます。

次の例は、 ReadSubtree メソッドの使用方法を示しています。

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 データを使用します。

<?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 は、要素ノードでのみ呼び出すことができます。 サブツリー全体が読み取られた場合、 Read メソッドの呼び出しは falseを返します。 新しい XML リーダーが閉じられると、元のリーダーはサブツリーの EndElement ノードに配置されます。 したがって、book 要素の開始タグに対して ReadSubtree メソッドを呼び出した場合、サブツリーが読み取られ、新しい XML リーダーが閉じられた後、元の XML リーダーは book 要素の終了タグに配置されます。

新しいリーダーが閉じられるまで、元のリーダーに対して操作を実行しないでください。 このアクションはサポートされていないため、予期しない動作が発生する可能性があります。

Note

ReadSubtree メソッドは、個別に操作できる XML データのコピーを作成するためのものではありません。 XML 要素の周囲に境界を作成するように設計されています。 これは、処理のためにデータを別のコンポーネントに渡す場合に、コンポーネントがアクセスできるデータの量を制限する場合に便利です。 ReadSubtree メソッドによって返された XML リーダーを別のアプリケーションに渡すと、アプリケーションは XML ドキュメント全体ではなく、その XML 要素にのみアクセスできます。

適用対象