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 呼叫 ,這個方法也會驗證傳回的內容

如同 在 中實作, XmlNodeReaderXmlTextReaderXmlValidatingReader 方法的 ReadOuterXml 類別是命名空間感知的。 假設有下列 XML 文字 <A xmlns:S="urn:1"><S:B>hello</S:B></A> ,如果讀取器位於開始標記上 S:BReadOuterXml 則會傳 <S:B xmlns:S="urn:1">hello<S:B/> 回 。

如需這個方法的非同步版本,請參閱 ReadOuterXmlAsync

適用於