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

如同 在 中實作, XmlNodeReaderXmlTextReaderXmlValidatingReader 方法的 ReadOuterXml 類別是命名空間感知的。

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

適用於