XmlValidatingReader.ReadAttributeValue Método

Definición

Analiza el valor de atributo en uno o varios nodos Text, EntityReference o 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 para devolver.

false si el lector no está situado en un nodo de atributo cuando se realiza la llamada inicial o si se leyeron todos los valores de atributo.

Un atributo vacío, como , misc=""devuelve true con un solo nodo con un valor de String.Empty.

Ejemplos

En el ejemplo siguiente se lee un atributo con nodos de referencia de texto y entidad.

#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

Comentarios

Nota:

La XmlValidatingReader clase está obsoleta en .NET Framework 2.0. Puede crear una instancia de XmlReader validación mediante la XmlReaderSettings clase y el Create método . Para obtener más información, vea la sección Comentarios de la página de referencia de XmlReader.

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. Los Depth incrementos y decrementos en uno al entrar y salir de las referencias de entidad generales.

Por ejemplo, supongamos que tiene el siguiente XML: <test name="a &b; c"/>

donde la entidad b se define en la definición de tipo de documento (DTD) de la siguiente manera: <!ENTITY b "123">

Si EntityHandling se establece ExpandCharEntitiesen , el código de C# siguiente devuelve el valor del atributo como dos nodos de texto y un nodo de referencia de entidad:

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"  
  }  
  }  
    }  

Se aplica a

Consulte también