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

String

Si le lecteur est placé sur un nœud d'élément ou 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

XML était incorrect ou une erreur s'est produite lors de l'analyse XML.

Une méthode XmlReader a été appelée avant la fin d’une opération asynchrone précédente. Dans ce cas, l’exception 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 retournée Position après l’appel
Element Sur l’étiquette de début item1. <item1>text1text2</item1><item2></item2> <item1>text1</item1> Sur l’étiquette de début item2.
Attribute Sur le nœud d'attribut attr1. <item attr1="val1" attr2="val2">texte</item> attr1="val1 » Reste sur le nœud d'attribut attr1.

Si le lecteur se trouve sur un nœud sans descendant, appeler la méthode ReadOuterXml équivaut à appeler la méthode 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 recherche des données XML bien formées. Si ReadOuterXml elle est appelée à partir d’un XmlValidatingReader, cette méthode valide également le contenu retourné

Comme implémenté dans le XmlNodeReader, XmlTextReader et XmlValidatingReader les classes de la méthode prennent en compte 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 à