XmlReader.ReadOuterXml メソッド

定義

派生クラスでオーバーライドされると、このノードとそのすべての子を表すマークアップを含むコンテンツを読み取ります。

public:
 virtual System::String ^ ReadOuterXml();
public virtual string ReadOuterXml();
abstract member ReadOuterXml : unit -> string
override this.ReadOuterXml : unit -> string
Public Overridable Function ReadOuterXml () As String

返品

リーダーが要素または属性ノードに配置されている場合、このメソッドは、現在のノードとそのすべての子のマークアップを含むすべての XML コンテンツを返します。それ以外の場合は、空の文字列を返します。

例外

XML が整形式でなかったか、XML の解析中にエラーが発生しました。

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

次の例では、 ReadInnerXml メソッドと ReadOuterXml メソッドを比較します。

// Load the file and ignore all white space.
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
using (XmlReader reader = XmlReader.Create("2books.xml")) {

  // Moves the reader to the root element.
  reader.MoveToContent();

  // Moves to book node.
  reader.Read();

  // Note that ReadInnerXml only returns the markup of the node's children
  // so the book's attributes are not returned.
  Console.WriteLine("Read the first book using ReadInnerXml...");
  Console.WriteLine(reader.ReadInnerXml());

  // ReadOuterXml returns the markup for the current node and its children
  // so the book's attributes are also returned.
  Console.WriteLine("Read the second book using ReadOuterXml...");
  Console.WriteLine(reader.ReadOuterXml());
}
' Load the file and ignore all white space.
Dim settings As New XmlReaderSettings()
settings.IgnoreWhitespace = True
Using reader As XmlReader = XmlReader.Create("2books.xml")

  ' Moves the reader to the root element.
  reader.MoveToContent()

  ' Moves to book node.
  reader.Read()

  ' Note that ReadInnerXml only returns the markup of the node's children
  ' so the book's attributes are not returned.
  Console.WriteLine("Read the first book using ReadInnerXml...")
  Console.WriteLine(reader.ReadInnerXml())

  ' ReadOuterXml returns the markup for the current node and its children
  ' so the book's attributes are also returned.
  Console.WriteLine("Read the second book using ReadOuterXml...")
  Console.WriteLine(reader.ReadOuterXml())

End Using

この例では、 2books.xml ファイルを入力として使用します。

<!--sample XML fragment-->
<bookstore>
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
</bookstore>

注釈

このメソッドは、開始タグと終了タグも返す点を除いて、 ReadInnerXml に似ています。

このメソッドは、次の方法で要素ノードと属性ノードを処理します。

ノード タイプ 呼び出し前の位置 XML フラグメント 戻り値 呼び出し後の位置
Element item1の開始タグ。 <item1>text1</item1><item2>text2</item2> <item1>text1</item1> item2の開始タグ。
Attribute attr1属性ノード。 <item attr1="val1" attr2="val2">text</item> attr1="val1" attr1属性ノードに残ります。

リーダーがリーフ ノードに配置されている場合、 ReadOuterXml の呼び出しは、 Readを呼び出すことと同じです。 このメソッドは String.Empty を返します (属性ノードを除き、その場合は属性マークアップが返されます)。

このメソッドは、整形式の XML をチェックします。 ReadOuterXmlXmlValidatingReaderから呼び出された場合、このメソッドは返されたコンテンツも検証します

XmlNodeReaderで実装されているように、XmlTextReader メソッドは名前空間に対応XmlValidatingReaderクラスとReadOuterXml クラスです。 次の XML テキスト <A xmlns:S="urn:1"><S:B>hello</S:B></A>を指定すると、リーダーが S:B 開始タグに配置された場合、 ReadOuterXml<S:B xmlns:S="urn:1">hello<S:B/>を返します。

このメソッドの非同期バージョンについては、 ReadOuterXmlAsyncを参照してください。

適用対象