Share via


XmlValidatingReader.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 riferimento di testo ed entità.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlValidatingReader^ 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 and set it to not expand general entities. 
      reader = gcnew XmlValidatingReader( xmlFrag,XmlNodeType::Element,context );
      reader->ValidationType = ValidationType::None;
      reader->EntityHandling = EntityHandling::ExpandCharEntities;
      
      //Read the misc attribute. Because EntityHandling is set to
      //ExpandCharEntities, 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 != nullptr )
            reader->Close();
   }

}

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

public class Sample
{
  public static void Main()
  {
    XmlValidatingReader 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 and set it to not expand general entities.
       reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
       reader.ValidationType = ValidationType.None;
       reader.EntityHandling = EntityHandling.ExpandCharEntities;

       //Read the misc attribute. Because EntityHandling is set to
       //ExpandCharEntities, 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 Strict
Option Explicit

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim reader As XmlValidatingReader = Nothing
        
        Try
            'Create the XML fragment to be parsed.
            Dim xmlFrag As String = "<book genre='novel' misc='sale-item &h; 1987'></book>"
            
            'Create the XmlParserContext.
            Dim context As XmlParserContext
            Dim subset As String = "<!ENTITY h 'hardcover'>"
            context = New XmlParserContext(Nothing, Nothing, "book", Nothing, Nothing, subset, "", "", XmlSpace.None)
            
            'Create the reader and set it to not expand general entities. 
            reader = New XmlValidatingReader(xmlFrag, XmlNodeType.Element, context)
            reader.ValidationType = ValidationType.None
            reader.EntityHandling = EntityHandling.ExpandCharEntities
            
            'Read the misc attribute. Because EntityHandling is set to
            'ExpandCharEntities, 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

Nota

La XmlValidatingReader classe è obsoleta in .NET Framework 2.0. È possibile creare un'istanza di convalida XmlReader usando la classe e il Create XmlReaderSettings metodo . Per altre informazioni, vedere la sezione Note della pagina di riferimento XmlReader.

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. Incrementi Depth e decrementi per uno quando si passa e fuori dai riferimenti di entità generali.

Si supponga, ad esempio, di avere il codice XML seguente: <test name="a &b; c"/>

dove l'entità b è definita nella definizione del tipo di documento (DTD) come indicato di seguito: <!ENTITY b "123">

Se EntityHandling è impostato su ExpandCharEntities, il codice C# seguente restituisce il valore dell'attributo come due nodi di testo e un nodo di riferimento di entità:

reader.MoveToAttribute("name");  
  while (reader.ReadAttributeValue())  
  {  
  if (reader.NodeType == XmlNodeType.Text)  
  {  
  // at this point reader.Value == "a " or " c"  
  }  
  else if (reader.NodeType == XmlNodeType.EntityReference)  
  {  
  // at this point reader.Name == "b"  
  reader.ResolveEntity();  
  while (reader.ReadAttributeValue() &&  
  reader.NodeType != XmlNodeType.EndEntity)  
  {  
  // reader.Value == "123"  
  }  
  }  
    }  

Si applica a

Vedi anche