XmlReader.ReadOuterXml Yöntem

Tanım

Türetilmiş bir sınıfta geçersiz kılındığında, bu düğümü ve tüm alt öğelerini temsil eden işaretleme de dahil olmak üzere içeriği okur.

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

Döndürülenler

String

Okuyucu bir öğeye veya öznitelik düğümüne konumlandırıldıysa, bu yöntem geçerli düğümün ve tüm alt öğelerinin işaretlemesi de dahil olmak üzere tüm XML içeriğini döndürür; aksi takdirde boş bir dize döndürür.

Özel durumlar

XML iyi biçimlendirilmemiş veya XML ayrıştırılırken bir hata oluştu.

Önceki XmlReader bir zaman uyumsuz işlem tamamlanmadan önce bir yöntem çağrıldı. Bu durumda, InvalidOperationException "Zaman uyumsuz bir işlem zaten devam ediyor" iletisiyle oluşturulur.

Örnekler

Aşağıdaki örnek ve ReadOuterXml yöntemlerini karşılaştırırReadInnerXml.

// 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

Örnekte girdi olarak dosya kullanılır 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>

Açıklamalar

Bu yöntem, başlangıç ve bitiş etiketlerini de döndürmesi dışında benzerdir ReadInnerXml .

Bu yöntem, öğe ve öznitelik düğümlerini aşağıdaki şekilde işler:

Düğüm türü Aramadan önceki konum XML parçası Döndürülen değer Aramadan Sonraki Konum
Element Başlangıç etiketinde item1 . <item1>metin1metin2</item1><item2></item2> <item1>metin1</item1> Başlangıç etiketinde item2 .
Attribute Öznitelik düğümünde attr1 . <item attr1="val1" attr2="val2">metin</item> attr1="val1" Öznitelik düğümünde attr1 kalır.

Okuyucu bir yaprak düğümde konumlandırıldıysa, çağrısı ReadOuterXml çağrısıyla Readeşdeğerdir. yöntemi döndürür String.Empty (öznitelik düğümleri dışında, bu durumda öznitelik işaretlemesi döndürülür).

Bu yöntem iyi biçimlendirilmiş XML'i denetler. bir XmlValidatingReaderiçinden çağrılırsaReadOuterXml, bu yöntem döndürülen içeriği de doğrular

ve XmlValidatingReader XmlTextReader sınıflarında XmlNodeReaderuygulandığı gibi yöntemi ad ReadOuterXml alanının farkındadır. Aşağıdaki XML metni <A xmlns:S="urn:1"><S:B>hello</S:B></A>verildiğinde, okuyucu başlangıç etiketine S:B konumlandırıldıysa döndürür ReadOuterXml <S:B xmlns:S="urn:1">hello<S:B/>.

Bu yöntemin zaman uyumsuz sürümü için bkz ReadOuterXmlAsync. .

Şunlara uygulanır