共用方式為


XmlTextReader.ReadAttributeValue 方法

定義

將屬性值剖析成一或多個 TextEntityReferenceEndEntity 節點。

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

傳回

Boolean

如果傳回的是節點,則為 true

如果在初次呼叫時讀取器不位於屬性節點上,或已讀取所有屬性值,則為 false

針對含有 String.Empty 值的單一節點,空白的屬性 (例如 misc="") 會傳回 true

範例

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

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ 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.
      reader = gcnew XmlTextReader( xmlFrag,XmlNodeType::Element,context );
      
      // 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 )
                  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()
  {
    XmlTextReader 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.
       reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);

       //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)
            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
Imports System.IO
Imports System.Xml

Public Class Sample

    Public Shared Sub Main()

        Dim reader As XmlTextReader = Nothing

        Try
            ' Create the XML fragment to be parsed.
            Dim xmlFrag As String = "<book genre='novel' misc='sale-item &h; 1987'></book>"
 
            Dim subset As String = "<!ENTITY h 'hardcover'>"
            ' Create the XmlParserContext.
            Dim context As New XmlParserContext(Nothing, Nothing, "book", Nothing, Nothing, subset, "", "", XmlSpace.None)
        
            'Create the reader.
            reader = New XmlTextReader(xmlFrag, XmlNodeType.Element, context)
  
            '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)
                    Console.WriteLine($"{reader.NodeType} {reader.Name}")
                Else
                    Console.WriteLine($"{reader.NodeType} {reader.Value}")
                End If
            End While

        Finally 
            If reader IsNot Nothing
                reader.Close()
            End if
        End Try
    End Sub
End Class

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

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

適用於

另請參閱