XmlValidatingReader.ReadAttributeValue 方法

定义

将属性值分析为一个或多个 TextEntityReferenceEndEntity 节点。

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

返回

Boolean

如果有可返回的节点,则为 true

如果进行初始调用时读取器不是定位在属性节点上,或者如果已读取了所有属性值,则为 false

如果是空属性,如 misc="",则返回 true,同时返回值为 String.Empty 的单个节点。

示例

以下示例读取具有文本和实体引用节点的属性。

#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

注解

备注

XmlValidatingReader在 .NET Framework 2.0 中已过时。 可以使用类和Create方法创建验证XmlReader实例XmlReaderSettings。 有关详细信息,请参阅 XmlReader 引用页的“备注”部分。

调用后使用 MoveToAttribute 此方法读取构成属性值的文本或实体引用节点。 Depth属性值节点是属性节点的深度之一。 Depth单步执行和退出常规实体引用时,递增和递减。

例如,假设具有以下 XML: <test name="a &b; c"/>

其中,实体 b 在文档类型定义中定义 (DTD) ,如下所示: <!ENTITY b "123">

如果 EntityHandling 设置为 ExpandCharEntities以下 C# 代码,则返回属性值作为两个文本节点和一个实体引用节点:

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

适用于

另请参阅