XmlReader.ReadInnerXml Metoda

Definicja

Po zastąpieniu w klasie pochodnej odczytuje całą zawartość, w tym znaczniki, jako ciąg.

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

Zwraca

Cała zawartość XML, w tym znaczniki, w bieżącym węźle. Jeśli bieżący węzeł nie ma elementów podrzędnych, zwracany jest pusty ciąg.

Jeśli bieżący węzeł nie jest elementem ani atrybutem, zwracany jest 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 zwraca całą zawartość bieżącego węzła, w tym znaczniki. Bieżący węzeł (tag początkowy) i odpowiadający mu węzeł końcowy (tag końcowy) nie są zwracane. Jeśli na przykład masz następujące elementy:

<node>
 this <child id="123"/>
</node>

ReadInnerXml Zwraca this <child id="123"/>

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 Położenie po wywołaniu
Element Na tagu startowym item1 . <item1 text1<>/item1><item2 text2<>/item2> text1 Na tagu startowym item2 .
Attribute W węźle atrybutu attr1 . <item attr1="val1" attr2="val2">text</item> val1 Pozostaje w węźle atrybutu attr1 .

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

Ta metoda sprawdza prawidłowo sformułowany kod XML. Jeśli ReadInnerXml 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.

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

Dotyczy