Assembly: System.Xml (in system.xml.dll)
Public Overridable Function ReadOuterXml As String
Dim instance As XmlReader Dim returnValue As String returnValue = instance.ReadOuterXml
public virtual string ReadOuterXml ()
public: virtual String^ ReadOuterXml ()
public String ReadOuterXml ()
public function ReadOuterXml () : String
Valore restituito
Se il visualizzatore è posizionato in corrispondenza di un nodo di elemento o di attributo, questo metodo restituirà tutto il contenuto XML del nodo corrente e dei relativi elementi figlio, incluso il codice; in caso contrario, restituirà una stringa vuota.| Tipo di eccezione | Condizione |
|---|---|
| L'XML non è in formato corretto oppure si è verificato un errore durante l'analisi dell'XML. |
Questo metodo è simile al metodo ReadInnerXml, ma a differenza di questo restituisce anche i tag di inizio e di fine.
Questo metodo gestisce i nodi attributo ed elemento come indicato di seguito:
| Tipo di nodo | Posizione prima della chiamata | Frammento XML | Valore restituito | Posizione dopo la chiamata |
|---|---|---|---|---|
| Element | In corrispondenza del tag di inizio item1. | <item1>text1</item1><item2>text2</item2> | <item1>text1</item1> | In corrispondenza del tag di inizio item2. |
| Attribute | In corrispondenza del nodo attributo attr1. | <item attr1="val1" attr2="val2">testo</item> | attr1="val1" | Rimane in corrispondenza del nodo attributo attr1. |
Se il visualizzatore è posizionato in corrispondenza di un nodo foglia, la chiamata al metodo ReadOuterXml equivarrà alla chiamata al metodo Read. Il metodo restituisce String.Empty (eccetto per i nodi attributo, nel qual caso viene restituito il codice dell'attributo).
Questo metodo verifica che la sintassi del codice XML sia corretta. Se il ReadOuterXml viene chiamato da un oggetto XmlValidatingReader, questo metodo convalida anche il contenuto restituito.
Per come è implementato nelle classi XmlNodeReader, XmlTextReader e XmlValidatingReader, il metodo ReadOuterXml riconosce gli spazi dei nomi. Dato il testo XML seguente <A xmlns:S="urn:1"><S:B>hello</S:B></A>, se il visualizzatore è posizionato sul tag di inizio S:B, ReadOuterXml restituisce <S:B xmlns:S="urn:1">hello<S:B/>.
Nell'esempio seguente vengono confrontati i metodi ReadInnerXml e 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
// 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()); }
Nell'esempio viene utilizzato il file 2books.xml 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>
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile per Pocket PC, Windows Mobile per Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema.
.NET Framework
Supportato in: 2.0 1.1 1.0.NET Compact Framework
Supportato in: 2.0 1.0Riferimenti
Classe XmlReaderMembri XmlReader
Spazio dei nomi System.Xml