XmlTextReader.ReadAttributeValue Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Analiza el valor del atributo en uno o varios Textnodos , EntityReferenceo EndEntity .
public:
override bool ReadAttributeValue();
public override bool ReadAttributeValue();
override this.ReadAttributeValue : unit -> bool
Public Overrides Function ReadAttributeValue () As Boolean
Devoluciones
true si hay nodos que se van a devolver.
false si el lector no está colocado en un nodo de atributo cuando se realiza la llamada inicial o si se han leído todos los valores de atributo.
Un atributo vacío, como , misc=""devuelve true con un único nodo con un valor de String.Empty.
Ejemplos
En el ejemplo siguiente se lee un atributo con nodos de texto y entidad.
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
Comentarios
Note
Se recomienda crear XmlReader instancias mediante el XmlReader.Create método para aprovechar las nuevas funcionalidades.
Use este método después de llamar MoveToAttribute a para leer los nodos de referencia de texto o entidad que componen el valor del atributo. El Depth de los nodos de valor de atributo es uno más la profundidad del nodo de atributo; incrementa y disminuye en uno cuando se introducen y salen de las referencias de entidad generales.