XmlReader.ReadInnerXml Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Quando sobrescrito numa classe derivada, lê todo o conteúdo, incluindo a marcação, como uma cadeia.
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
Devoluções
Todo o conteúdo XML, incluindo a marcação, está no nó atual. Se o nó atual não tiver filhos, uma cadeia vazia é devolvida.
Se o nó atual não for nem um elemento nem um atributo, uma cadeia vazia é devolvida.
Exceções
O XML não estava bem formado, ou ocorreu um erro durante a análise do XML.
Um XmlReader método era chamado antes de uma operação assíncrona anterior terminar. Neste caso, InvalidOperationException é lançado com a mensagem "Uma operação assíncrona já está em curso."
Exemplos
O exemplo seguinte compara os ReadInnerXml métodos e 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
O exemplo usa 2books.xml ficheiro 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>
Observações
Este método devolve todo o conteúdo do nó atual, incluindo a marcação. O nó atual (etiqueta inicial) e o nó final correspondente (etiqueta final) não são devolvidos. Por exemplo, se tivesse o seguinte:
<node>
this <child id="123"/>
</node>
ReadInnerXml retorna this <child id="123"/>
Este método trata os nós de elemento e atributo da seguinte forma:
| Tipo de nó | Posição antes da chamada | Fragmento XML | Valor de retorno | Posição após a chamada |
|---|---|---|---|---|
Element |
Na item1 etiqueta de início. |
<Item1>texto1/<item1><item2>texto2</item2> | texto1 | Na item2 etiqueta de início. |
Attribute |
No attr1 nó de atributo. |
<item attr1="val1" attr2="val2">texto</item> | Val1 | Permanece no attr1 nó de atributo. |
Se o leitor estiver posicionado num nó folha, chamar ReadInnerXml é equivalente a chamar Read. O método retorna String.Empty (exceto para nós de atributo, caso em que o valor do atributo é devolvido).
Este método verifica se XML está bem formado. Se ReadInnerXml for chamado a partir de um XmlValidatingReader, este método também valida o conteúdo devolvido.
Tal como implementado nas XmlNodeReaderclasses , XmlTextReader e XmlValidatingReader o ReadOuterXml método é consciente do namespace.
Para a versão assíncrona deste método, veja ReadInnerXmlAsync.