XmlReader.ReadOuterXml 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 sobreposto numa classe derivada, lê o conteúdo, incluindo a marcação, representando este nó e todos os seus filhos.
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
Devoluções
Se o leitor estiver posicionado num elemento ou num nó de atributo, este método devolve todo o conteúdo XML, incluindo a marcação, do nó atual e de todos os seus filhos; caso contrário, devolve uma cadeia vazia.
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 é semelhante, ReadInnerXml exceto que também devolve as etiquetas de início e fim.
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> | <Item1>texto1/<item1> | Na item2 etiqueta de início. |
Attribute |
No attr1 nó de atributo. |
<item attr1="val1" attr2="val2">texto</item> | attr1="val1" | Permanece no attr1 nó de atributo. |
Se o leitor estiver posicionado num nó folha, chamar ReadOuterXml é equivalente a chamar Read. O método devolve String.Empty (exceto para nós de atributo, caso em que a marcação de atributo é devolvida).
Este método verifica se XML está bem formado. Se ReadOuterXml 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. Dado o seguinte texto <A xmlns:S="urn:1"><S:B>hello</S:B></A>XML , se o leitor estiver posicionado na S:B etiqueta inicial, retorna ReadOuterXml<S:B xmlns:S="urn:1">hello<S:B/>.
Para a versão assíncrona deste método, veja ReadOuterXmlAsync.