XmlTextReader.ReadAttributeValue Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Analisa o valor do atributo em um ou mais Text, EntityReference, ou EndEntity nós.
public:
override bool ReadAttributeValue();
public override bool ReadAttributeValue();
override this.ReadAttributeValue : unit -> bool
Public Overrides Function ReadAttributeValue () As Boolean
Devoluções
true se houver nós para devolver.
false se o leitor não estiver posicionado num nó de atributo quando a chamada inicial é feita ou se todos os valores do atributo foram lidos.
Um atributo vazio, como misc="", devolve true com um único nó com valor de String.Empty.
Exemplos
O exemplo seguinte lê um atributo com nós de texto e entidade.
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
Observações
Note
Recomendamos que crie instâncias XmlReader usando o método XmlReader.Create para aproveitar a nova funcionalidade.
Use este método após chamar MoveToAttribute para ler os nós de referência de texto ou entidade que compõem o valor do atributo. O Depth dos nós do valor do atributo é um mais a profundidade do nó do atributo; ele incrementa e diminui em um ao entrar e sair de referências gerais de entidades.