共用方式為


XmlNodeReader.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()
{
   XmlNodeReader^ reader = nullptr;
   try
   {
      
      //Create and load an XML document.
      XmlDocument^ doc = gcnew XmlDocument;
      doc->LoadXml( "<!DOCTYPE book [<!ENTITY h 'harcover'>]>"
      "<book genre='novel' misc='sale-item &h; 1987'>"
      "</book>" );
      
      //Create the reader. 
      reader = gcnew XmlNodeReader( doc );
      
      //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 )
                  
         //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()
  {
    XmlNodeReader reader = null;

    try
    {
       //Create and load an XML document.
       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'harcover'>]>" +
                   "<book genre='novel' misc='sale-item &h; 1987'>" +
                   "</book>");

       //Create the reader.
       reader = new XmlNodeReader(doc);

       //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)
            //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 Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim reader As XmlNodeReader = Nothing
        
        Try
            'Create and load an XML document.
            Dim doc As New XmlDocument()
            doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'harcover'>]>" & _
                        "<book genre='novel' misc='sale-item &h; 1987'>" & _
                        "</book>")
            
            'Create the reader. 
            reader = New XmlNodeReader(doc)
            
            '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 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

備註

注意

在 .NET Framework 2.0 中,建議的做法是使用 XmlReaderSettings 類別和 Create 方法建立 XmlReader 實例。 這可讓您充分利用.NET Framework中引進的所有新功能。 如需詳細資訊,請參閱參考頁面中的 XmlReader 一節。

呼叫 MoveToAttribute 之後,請使用此方法來讀取構成屬性值的文字或實體參考節點。 Depth屬性值節點的 是屬性節點的一個加上深度;當您逐步進入和離開一般實體參考時,它會遞增和遞減一個節點。

適用於