Condividi tramite


XmlNodeReader.ReadAttributeValue Metodo

Definizione

Analizza il valore dell'attributo in uno o più Textnodi , EntityReferenceo EndEntity .

public:
 override bool ReadAttributeValue();
public override bool ReadAttributeValue();
override this.ReadAttributeValue : unit -> bool
Public Overrides Function ReadAttributeValue () As Boolean

Restituisce

true se sono presenti nodi da restituire.

false se il lettore non è posizionato su un nodo dell'attributo quando viene eseguita la chiamata iniziale o se tutti i valori dell'attributo sono stati letti.

Un attributo vuoto, ad esempio , misc=""restituisce true con un singolo nodo con un valore String.Empty.

Esempio

Nell'esempio seguente viene letto un attributo con nodi di riferimento di testo ed entità.

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlNodeReader reader = null;

    try
    {
       //Create and load an XML document.
       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'harcover'>]>" +
                   "<book genre='novel' misc='sale-item &h; 1987'>" +
                   "</book>");

       //Create the reader.
       reader = new XmlNodeReader(doc);

       //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)
            //To expand the entity, call ResolveEntity.
            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
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim reader As XmlNodeReader = Nothing
        
        Try
            'Create and load an XML document.
            Dim doc As New XmlDocument()
            doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'harcover'>]>" & _
                        "<book genre='novel' misc='sale-item &h; 1987'>" & _
                        "</book>")
            
            'Create the reader. 
            reader = New XmlNodeReader(doc)
            
            '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 Then
                    'To expand the entity, call ResolveEntity.
                    Console.WriteLine("{0} {1}", reader.NodeType, reader.Name)
                Else
                    Console.WriteLine("{0} {1}", reader.NodeType, reader.Value)
                End If
            End While
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub
End Class

Commenti

Annotazioni

Per sfruttare le funzionalità più recenti, la procedura consigliata consiste nel creare XmlReader istanze usando la XmlReaderSettings classe e il Create metodo . Per altre informazioni, vedere la sezione Osservazioni nella XmlReader pagina di riferimento.

Usare questo metodo dopo aver chiamato MoveToAttribute per leggere i nodi di riferimento di testo o entità che costituiscono il valore dell'attributo. L'oggetto Depth dei nodi del valore dell'attributo è uno più la profondità del nodo dell'attributo; incrementa e decrementa di uno quando si eseguono riferimenti alle entità generali.

Si applica a