XmlNodeReader.GetAttribute 方法

定義

取得屬性值。

多載

GetAttribute(Int32)

取得具有指定索引的屬性值。

GetAttribute(String)

取得具有指定名稱的屬性值。

GetAttribute(String, String)

取得具有指定的區域名稱和命名空間 URI 的屬性值。

GetAttribute(Int32)

取得具有指定索引的屬性值。

public:
 override System::String ^ GetAttribute(int attributeIndex);
public override string GetAttribute (int attributeIndex);
override this.GetAttribute : int -> string
Public Overrides Function GetAttribute (attributeIndex As Integer) As String

參數

attributeIndex
Int32

屬性的索引。 此索引是以零為起始。 (第一個屬性的索引為 0。)

傳回

String

指定的屬性值。

例外狀況

i 參數小於 0,或大於或等於 AttributeCount

備註

注意

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

這個方法不會移動讀取器。

適用於

GetAttribute(String)

取得具有指定名稱的屬性值。

public:
 override System::String ^ GetAttribute(System::String ^ name);
public override string? GetAttribute (string name);
public override string GetAttribute (string name);
override this.GetAttribute : string -> string
Public Overrides Function GetAttribute (name As String) As String

參數

name
String

屬性的限定名稱 (Qualified Name)。

傳回

String

指定的屬性值。 如果找不到屬性,會傳回 null

範例

下列範例會取得 ISBN 屬性的值。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlNodeReader^ reader = nullptr;
   try
   {
      
      //Create and load the XML document.
      XmlDocument^ doc = gcnew XmlDocument;
      doc->LoadXml( "<book genre='novel' ISBN='1-861003-78' publicationdate='1987'> "
      "</book>" );
      
      // Load the XmlNodeReader 
      reader = gcnew XmlNodeReader( doc );
      
      //Read the ISBN attribute.
      reader->MoveToContent();
      String^ isbn = reader->GetAttribute( "ISBN" );
      Console::WriteLine( "The ISBN value: {0}", isbn );
   }
   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 the XML document.
       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<book genre='novel' ISBN='1-861003-78' publicationdate='1987'> " +
                   "</book>");

       // Load the XmlNodeReader
       reader = new XmlNodeReader(doc);

       //Read the ISBN attribute.
       reader.MoveToContent();
       string isbn = reader.GetAttribute("ISBN");
       Console.WriteLine("The ISBN value: " + isbn);
     }

     finally
     {
        if (reader != null)
          reader.Close();
      }
  }
} // End class
Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim reader As XmlNodeReader = Nothing
        
        Try
            'Create and load the XML document.
            Dim doc As New XmlDocument()
            doc.LoadXml("<book genre='novel' ISBN='1-861003-78' publicationdate='1987'> " & _
                       "</book>")
            
            ' Load the XmlNodeReader 
            reader = New XmlNodeReader(doc)
            
            'Read the ISBN attribute.
            reader.MoveToContent()
            Dim isbn As String = reader.GetAttribute("ISBN")
            Console.WriteLine("The ISBN value: " & isbn)
        
        
        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 節。

這個方法不會移動讀取器。

如果讀取器位於 DocumentType 節點上,這個方法可用來取得 PUBLIC 和 SYSTEM 常值,例如 reader.GetAttribute("PUBLIC")

適用於

GetAttribute(String, String)

取得具有指定的區域名稱和命名空間 URI 的屬性值。

public:
 override System::String ^ GetAttribute(System::String ^ name, System::String ^ namespaceURI);
public override string? GetAttribute (string name, string? namespaceURI);
public override string GetAttribute (string name, string namespaceURI);
override this.GetAttribute : string * string -> string
Public Overrides Function GetAttribute (name As String, namespaceURI As String) As String

參數

name
String

屬性的本機名稱。

namespaceURI
String

屬性的命名空間 URI。

傳回

String

指定的屬性值。 如果找不到屬性,會傳回 null

備註

注意

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

下列 XML 包含特定命名空間中的屬性:

<test xmlns:dt="urn:datatypes" dt:type="int"/>  

您可以使用一個引數 (前置詞和區功能變數名稱稱) 或兩個引數來查閱 dt:type 屬性, (本機名稱和命名空間 URI) :

String dt = reader.GetAttribute("dt:type");  
String dt2 = reader.GetAttribute("type","urn:datatypes");  

若要查閱 xmlns:dt 屬性,請使用下列其中一個引數:

String dt3 = reader.GetAttribute("xmlns:dt");  
String dt4 = reader.GetAttribute("dt",http://www.w3.org/2000/xmlns/);  

您也可以使用 Prefix 屬性取得這項資訊。

適用於