XmlReader.ReadOuterXml Metoda

Definicja

Po zastąpieniu w klasie pochodnej odczytuje zawartość, w tym znaczniki, reprezentując ten węzeł i wszystkie jego elementy podrzędne.

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

Zwraca

Jeśli czytnik jest umieszczony w elemencie lub węźle atrybutu, ta metoda zwraca całą zawartość XML, w tym znaczniki, bieżącego węzła i wszystkich jego elementów podrzędnych; w przeciwnym razie zwraca pusty ciąg.

Wyjątki

Kod XML nie został poprawnie sformułowany lub wystąpił błąd podczas analizowania kodu XML.

Metoda XmlReader została wywołana przed zakończeniem poprzedniej operacji asynchronicznej. W takim przypadku InvalidOperationException jest zgłaszany komunikat "Operacja asynchroniczna jest już w toku".

Przykłady

Poniższy przykład porównuje ReadInnerXml metody i 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

W przykładzie użyto 2books.xml pliku jako danych wejściowych.

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

Uwagi

Ta metoda jest podobna do ReadInnerXml tej, z tą różnicą, że zwraca również tagi początkowe i końcowe.

Ta metoda obsługuje węzły elementów i atrybutów w następujący sposób:

Typ węzła Położenie przed wywołaniem Fragment XML Wartość zwracana Pozycja po wywołaniu
Element Na tagu startowym item1 . <item1 text1<>/item1><item2 text2<>/item2> <item1 text1></item1> Na tagu startowym item2 .
Attribute W węźle atrybutu attr1 . <item attr1="val1" attr2="val2">text</item> attr1="val1" Pozostaje w węźle atrybutu attr1 .

Jeśli czytnik jest umieszczony w węźle liścia, wywołanie ReadOuterXml jest równoważne wywołaniu metody Read. Metoda zwraca String.Empty wartość (z wyjątkiem węzłów atrybutów, w tym przypadku zwracany jest znacznik atrybutu).

Ta metoda sprawdza prawidłowo sformułowany kod XML. Jeśli ReadOuterXml metoda jest wywoływana XmlValidatingReaderz klasy , ta metoda weryfikuje również zwróconą zawartość

Jak zaimplementowano w metodzie XmlNodeReader, XmlTextReader i XmlValidatingReader klasa jest świadoma ReadOuterXml przestrzeni nazw. Biorąc pod uwagę następujący tekst <A xmlns:S="urn:1"><S:B>hello</S:B></A>XML , jeśli czytnik został umieszczony w tagu startowym S:B , ReadOuterXml zwraca wartość <S:B xmlns:S="urn:1">hello<S:B/>.

Aby uzyskać asynchroniczną wersję tej metody, zobacz ReadOuterXmlAsync.

Dotyczy