XmlReader.ReadOuterXml Metodo

Definizione

Quando sottoposto a override in una classe derivata, legge il contenuto, incluso il markup, che rappresenta questo nodo e tutti i relativi elementi figlio.

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

Valori restituiti

Se il lettore è posizionato su un elemento o su un nodo di attributo, questo metodo restituisce tutto il contenuto XML, incluso il markup, del nodo corrente e di tutti i relativi elementi figlio; in caso contrario, restituisce una stringa vuota.

Eccezioni

Il codice XML non è corretto o si è verificato un errore durante l'analisi del codice XML.

È stato chiamato un XmlReader metodo prima del completamento di un'operazione asincrona precedente. In questo caso, InvalidOperationException viene generata con il messaggio "Un'operazione asincrona è già in corso".

Esempio

Nell'esempio seguente vengono confrontati i ReadInnerXml metodi 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

Nell'esempio viene 2books.xml usato il file come input.

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

Commenti

Questo metodo è simile a ReadInnerXml tranne che restituisce anche i tag iniziale e finale.

Questo metodo gestisce i nodi di elemento e attributo nel modo seguente:

Tipo di nodo Posizione prima della chiamata Frammento XML Valore restituito Posizione dopo la chiamata
Element item1 Nel tag start. <item1>text1</item1><item2 text2<>/item2> <item1 text1></item1> item2 Nel tag start.
Attribute Nel nodo dell'attributo attr1 . <item attr1="val1" attr2="val2">text</item> attr1="val1" Rimane nel nodo dell'attributo attr1 .

Se il lettore è posizionato in un nodo foglia, la chiamata ReadOuterXml equivale a chiamare Read. Il metodo restituisce String.Empty (ad eccezione dei nodi dell'attributo, nel qual caso viene restituito il markup dell'attributo).

Questo metodo controlla la presenza di codice XML ben formato. Se ReadOuterXml viene chiamato da un oggetto XmlValidatingReader, questo metodo convalida anche il contenuto restituito

Come implementato nelle XmlNodeReaderclassi , XmlTextReader e XmlValidatingReader il metodo è compatibile con lo ReadOuterXml spazio dei nomi. Dato il testo <A xmlns:S="urn:1"><S:B>hello</S:B></A>XML seguente, se il lettore è stato posizionato sul S:B tag iniziale, ReadOuterXml restituisce <S:B xmlns:S="urn:1">hello<S:B/>.

Per la versione asincrona di questo metodo, vedere ReadOuterXmlAsync.

Si applica a