XmlReader.ReadOuterXml Méthode

Définition

En cas de substitution dans une classe dérivée, lit le contenu, y compris le balisage, représentant ce nœud et tous ses enfants.

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

Retours

Si le lecteur est positionné sur un élément ou un nœud d’attribut, cette méthode retourne tout le contenu XML, y compris le balisage, du nœud actuel et de tous ses enfants ; sinon, elle retourne une chaîne vide.

Exceptions

Le code XML n’a pas été correctement formé ou une erreur s’est produite lors de l’analyse du code XML.

Une XmlReader méthode a été appelée avant la fin d’une opération asynchrone précédente. Dans ce cas, InvalidOperationException est levée avec le message « Une opération asynchrone est déjà en cours ».

Exemples

L’exemple suivant compare les méthodes et ReadOuterXml les ReadInnerXml méthodes.

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

L’exemple utilise 2books.xml le fichier comme entrée.

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

Remarques

Cette méthode est similaire à celle-ci, ReadInnerXml sauf qu’elle retourne également les balises de début et de fin.

Cette méthode gère les nœuds d’élément et d’attribut de la manière suivante :

Type de nœud Position avant l’appel Fragment XML Valeur renvoyée Position après l’appel
Element Sur la balise de item1 début. <item1 text1<>/item1><item2 text2<>/item2/item2> <item1 text1></item1> Sur la balise de item2 début.
Attribute Sur le nœud d’attribut attr1 . <item attr1="val1 » attr2="val2">text</item> attr1="val1 » Reste sur le nœud d’attribut attr1 .

Si le lecteur est positionné sur un nœud feuille, l’appel ReadOuterXml équivaut à appeler Read. La méthode retourne String.Empty (à l’exception des nœuds d’attribut, auquel cas le balisage d’attribut est retourné).

Cette méthode vérifie le xml bien formé. Si ReadOuterXml elle est appelée à partir d’un XmlValidatingReader, cette méthode valide également le contenu retourné

Comme implémenté dans les classes, XmlValidatingReaderXmlTextReader la XmlNodeReaderméthode est consciente de l’espace ReadOuterXml de noms. Étant donné le texte <A xmlns:S="urn:1"><S:B>hello</S:B></A>XML suivant, si le lecteur était positionné sur la S:B balise de début, ReadOuterXml retourne <S:B xmlns:S="urn:1">hello<S:B/>.

Pour obtenir la version asynchrone de cette méthode, consultez ReadOuterXmlAsync.

S’applique à