XmlReader.ReadOuterXml メソッド
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
派生クラスでオーバーライドされると、このノードとそのすべての子を表す内容 (マークアップを含む) を読み取ります。
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 がスローされます。
次の例では、 メソッドと ReadOuterXml
メソッドをReadInnerXml
比較します。
// 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 をチェックします。 が から呼び出された場合 ReadOuterXml
、 XmlValidatingReaderこのメソッドは返されるコンテンツも検証します
および クラスにXmlNodeReaderXmlTextReaderXmlValidatingReader
実装されているように、 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。
製品 | バージョン |
---|---|
.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, 10 |
.NET Framework | 1.1, 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 に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。