XmlReader.ReadInnerXml Méthode

Définition

En cas de substitution dans une classe dérivée, lit tout le contenu, y compris le balisage, sous forme de chaîne.

public:
 virtual System::String ^ ReadInnerXml();
public virtual string ReadInnerXml();
abstract member ReadInnerXml : unit -> string
override this.ReadInnerXml : unit -> string
Public Overridable Function ReadInnerXml () As String

Retours

Tout le contenu XML, y compris le balisage, dans le nœud actuel. Si le nœud actuel n’a pas d’enfants, une chaîne vide est retournée.

Si le nœud actuel n’est ni un élément ni un attribut, une chaîne vide est retournée.

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 retourne tout le contenu du nœud actuel, y compris le balisage. Le nœud actuel (balise de début) et le nœud final correspondant (balise de fin) ne sont pas retournés. Par exemple, si vous avez les éléments suivants :

<node>
 this <child id="123"/>
</node>

ReadInnerXml retourne « this <child id="123"/> »

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> text1 Sur la balise de item2 début.
Attribute Sur le nœud d’attribut attr1 . <item attr1="val1 » attr2="val2">text</item> val1 Reste sur le nœud d’attribut attr1 .

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

Cette méthode vérifie le xml bien formé. Si ReadInnerXml 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.

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

S’applique à