XmlTextReader.ReadAttributeValue Metodo

Definizione

Analizza il valore dell'attributo in uno o più nodi Text, EntityReference o EndEntity.

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

Restituisce

Boolean

true se sono presenti nodi da restituire.

false se il lettore non è posizionato in corrispondenza di un nodo attributo quando viene effettuata la chiamata iniziale oppure se è stato letto il valore di tutti gli attributi.

Un attributo vuoto, quale misc="", restituisce true con un singolo nodo il cui valore è String.Empty.

Esempio

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

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = nullptr;
   __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 = gcnew XmlParserContext( nullptr,nullptr,"book",nullptr,nullptr,subset,"","",XmlSpace::None );
      
      // Create the reader.
      reader = gcnew 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 != nullptr )
            reader->Close();
   }

}
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

Commenti

Nota

A partire da .NET Framework 2.0, è consigliabile creare XmlReader istanze usando il XmlReader.Create metodo per sfruttare nuove funzionalità.

Usare questo metodo dopo aver chiamato MoveToAttribute per leggere i nodi di riferimento di testo o entità che costituiscono il valore dell'attributo. I Depth nodi del valore dell'attributo sono uno più la profondità del nodo dell'attributo. Incrementa e decrementa per uno quando si passa e fuori dai riferimenti di entità generali.

Si applica a

Vedi anche