XmlReader.ReadOuterXml Método

Definición

Cuando se reemplaza en una clase derivada, lee el contenido, incluido el marcado, que representa este nodo y todos sus elementos secundarios.

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

Devoluciones

Si el lector se coloca en un elemento o en un nodo de atributo, este método devuelve todo el contenido XML, incluido el marcado, del nodo actual y todos sus elementos secundarios; de lo contrario, devuelve una cadena vacía.

Excepciones

El XML no se ha formado correctamente o se produjo un error al analizar el XML.

Se llamó a un XmlReader método antes de que finalice una operación asincrónica anterior. En este caso, InvalidOperationException se produce con el mensaje "Una operación asincrónica ya está en curso".

Ejemplos

En el ejemplo siguiente se comparan los ReadInnerXml métodos y 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

En el ejemplo se usa 2books.xml el archivo como entrada.

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

Comentarios

Este método es similar a ReadInnerXml , salvo que también devuelve las etiquetas start y end.

Este método controla los nodos de elemento y atributo de la siguiente manera:

Tipo de nodo Posición antes de la llamada Fragmento XML Valor devuelto Posición después de la llamada
Element En la item1 etiqueta de inicio. <item1 text1<>/item1><item2 text2<>/item2> <item1 text1></item1> En la item2 etiqueta de inicio.
Attribute En el nodo de attr1 atributo. <item attr1="val1" attr2="val2">text</item> attr1="val1" Permanece en el nodo de attr1 atributo.

Si el lector se coloca en un nodo hoja, la llamada ReadOuterXml equivale a llamar a Read. El método devuelve String.Empty (excepto los nodos de atributo, en cuyo caso se devuelve el marcado de atributo).

Este método comprueba si hay XML bien formado. Si ReadOuterXml se llama desde un XmlValidatingReader, este método también valida el contenido devuelto.

Como se implementa en las XmlNodeReaderclases , XmlTextReader y XmlValidatingReader el método es compatible con el ReadOuterXml espacio de nombres. Dado el siguiente texto <A xmlns:S="urn:1"><S:B>hello</S:B></A>XML, si el lector se colocó en la S:B etiqueta de inicio, ReadOuterXml devuelve <S:B xmlns:S="urn:1">hello<S:B/>.

Para obtener la versión asincrónica de este método, vea ReadOuterXmlAsync.

Se aplica a