XmlReader.ReadOuterXml Método
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Quando substituído em uma classe derivada, lê o conteúdo, incluindo marcação, representando esse 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
Retornos
Se o leitor estiver posicionado em um elemento ou um nó de atributo, esse método retornará todo o conteúdo XML, incluindo marcação, do nó atual e de todos os seus filhos; caso contrário, ele retorna uma cadeia de caracteres vazia.
Exceções
O XML não foi bem formado ou ocorreu um erro ao analisar o XML.
Um XmlReader método foi chamado antes de uma operação assíncrona anterior ser concluída. Nesse caso, InvalidOperationException é lançada com a mensagem "Uma operação assíncrona já está em andamento".
Exemplos
O exemplo a seguir compara os métodos e ReadOuterXml os ReadInnerXml métodos.
// 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 o 2books.xml arquivo 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>
Comentários
Esse método é semelhante a ReadInnerXml , exceto que também retorna as marcas de início e de término.
Esse método manipula nós de elemento e atributo da seguinte maneira:
| Tipo de nó | Posição antes da chamada | Fragmento XML | Valor de devolução | Posição após a chamada |
|---|---|---|---|---|
Element |
item1 Na marca inicial. |
<item1>text1</item1><item2>text2</item2> | <item1>text1</item1> |
item2 Na marca inicial. |
Attribute |
No nó de attr1 atributo. |
<item attr1="val1" attr2="val2">text</item> | attr1="val1" | Permanece no nó de attr1 atributo. |
Se o leitor estiver posicionado em um nó folha, a chamada ReadOuterXml será equivalente à chamada Read. O método retorna String.Empty (exceto para nós de atributo, nesse caso, a marcação de atributo é retornada).
Esse método verifica se há XML bem formado. Se ReadOuterXml for chamado de um XmlValidatingReader, esse método também validará o conteúdo retornado
Conforme implementado na XmlNodeReaderclasse , XmlTextReader o método tem reconhecimento de ReadOuterXml namespace.XmlValidatingReader Dado o seguinte texto <A xmlns:S="urn:1"><S:B>hello</S:B></A>XML, se o leitor tiver sido posicionado na S:B marca inicial, retornará <S:B xmlns:S="urn:1">hello<S:B/>ReadOuterXml .
Para obter a versão assíncrona deste método, consulte ReadOuterXmlAsync.