共用方式為


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="") 會對含有 String.Empty 值的單一節點傳回 true

範例

下列範例會讀取具有文字和實體參考節點的屬性。

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

適用於

另請參閱