XmlReader.ReadInnerXml Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Cuando se invalida en una clase derivada, lee todo el contenido, incluido el marcado, como una cadena.
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
Devoluciones
Todo el contenido XML, incluido el marcado, en el nodo actual. Si el nodo actual no tiene elementos secundarios, se devuelve una cadena vacía.
Si el nodo actual no es ningún elemento ni atributo, se 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 devuelve todo el contenido del nodo actual, incluido el marcado. No se devuelve el nodo actual (etiqueta de inicio) y el nodo final correspondiente (etiqueta final). Por ejemplo, si tenía lo siguiente:
<node>
this <child id="123"/>
</node>
ReadInnerXml devuelve this <child id="123"/>.
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> | texto1 | En la item2 etiqueta de inicio. |
Attribute |
En el nodo de attr1 atributo. |
<item attr1="val1" attr2="val2">text</item> | val1 | Permanece en el nodo de attr1 atributo. |
Si el lector se coloca en un nodo hoja, la llamada ReadInnerXml equivale a llamar a Read. El método devuelve String.Empty (excepto los nodos de atributo, en cuyo caso se devuelve el valor del atributo).
Este método comprueba si hay XML bien formado. Si ReadInnerXml 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.
Para obtener la versión asincrónica de este método, vea ReadInnerXmlAsync.