言語

XmlReader.ReadInnerXml メソッド

定義

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

public:
 virtual System::String ^ ReadInnerXml();
public virtual string ReadInnerXml();
abstract member ReadInnerXml : unit -> string
override this.ReadInnerXml : unit -> string
Public Overridable Function ReadInnerXml () 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>

注釈

このメソッドは、マークアップを含む現在のノードのすべてのコンテンツを返します。 現在のノード (開始タグ) と対応する終了ノード (終了タグ) は返されません。 たとえば、次のような場合です。

<node>
 this <child id="123"/>
</node>

ReadInnerXml は this <child id="123"/> を返します。

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

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

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

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

XmlNodeReaderで実装されているように、XmlTextReader メソッドは名前空間に対応XmlValidatingReaderクラスとReadOuterXml クラスです。

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

適用対象