XmlTextReader.ReadAttributeValue Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Analyse la valeur d’attribut en un ou plusieurs TextEntityReferencenœuds.EndEntity
public:
override bool ReadAttributeValue();
public override bool ReadAttributeValue();
override this.ReadAttributeValue : unit -> bool
Public Overrides Function ReadAttributeValue () As Boolean
Retours
true s’il existe des nœuds à retourner.
false si le lecteur n’est pas positionné sur un nœud d’attribut lorsque l’appel initial est effectué ou si toutes les valeurs d’attribut ont été lues.
Un attribut vide, tel que , misc=""retourne true avec un nœud unique avec une valeur de String.Empty.
Exemples
L’exemple suivant lit un attribut avec du texte et des nœuds d’entité.
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlTextReader reader = null;
try
{
//Create the XML fragment to be parsed.
string xmlFrag ="<book genre='novel' misc='sale-item &h; 1987'></book>";
//Create the XmlParserContext.
XmlParserContext context;
string subset = "<!ENTITY h 'hardcover'>";
context = new XmlParserContext(null, null, "book", null, null, subset, "", "", XmlSpace.None);
//Create the reader.
reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);
//Read the misc attribute. The attribute is parsed
//into multiple text and entity reference nodes.
reader.MoveToContent();
reader.MoveToAttribute("misc");
while (reader.ReadAttributeValue()){
if (reader.NodeType==XmlNodeType.EntityReference)
Console.WriteLine("{0} {1}", reader.NodeType, reader.Name);
else
Console.WriteLine("{0} {1}", reader.NodeType, reader.Value);
}
}
finally
{
if (reader != null)
reader.Close();
}
}
} // End class
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
Dim reader As XmlTextReader = Nothing
Try
' Create the XML fragment to be parsed.
Dim xmlFrag As String = "<book genre='novel' misc='sale-item &h; 1987'></book>"
Dim subset As String = "<!ENTITY h 'hardcover'>"
' Create the XmlParserContext.
Dim context As New XmlParserContext(Nothing, Nothing, "book", Nothing, Nothing, subset, "", "", XmlSpace.None)
'Create the reader.
reader = New XmlTextReader(xmlFrag, XmlNodeType.Element, context)
'Read the misc attribute. The attribute is parsed
'into multiple text and entity reference nodes.
reader.MoveToContent()
reader.MoveToAttribute("misc")
While (reader.ReadAttributeValue())
If (reader.NodeType = XmlNodeType.EntityReference)
Console.WriteLine($"{reader.NodeType} {reader.Name}")
Else
Console.WriteLine($"{reader.NodeType} {reader.Value}")
End If
End While
Finally
If reader IsNot Nothing
reader.Close()
End if
End Try
End Sub
End Class
Remarques
Note
Nous vous recommandons de créer des instances XmlReader à l’aide de la méthode XmlReader.Create pour tirer parti de nouvelles fonctionnalités.
Utilisez cette méthode après avoir appelé MoveToAttribute pour lire les nœuds de référence de texte ou d’entité qui composent la valeur d’attribut. Les Depth nœuds de valeur d’attribut sont un nœud plus la profondeur du nœud d’attribut ; il incrémente et décrémente par un lorsque vous effectuez un pas à pas dans et hors des références d’entité générales.