XmlTextReader.GetAttribute 方法

定義

取得屬性值。

多載

GetAttribute(Int32)

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

GetAttribute(String)

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

GetAttribute(String, String)

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

備註

注意

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

GetAttribute(Int32)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

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

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

參數

i
Int32

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

傳回

指定的屬性值。

例外狀況

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

備註

注意

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

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

另請參閱

適用於

GetAttribute(String)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

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

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)。

傳回

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

範例

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

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = nullptr;
   try
   {
      
      //Load the reader with the XML file.
      reader = gcnew XmlTextReader( "attrs.xml" );
      
      //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()
  {
    XmlTextReader reader = null;

    try
    {
       //Load the reader with the XML file.
       reader = new XmlTextReader("attrs.xml");

       //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 XmlTextReader = Nothing
        
        Try
            'Load the reader with the XML file.
            reader = New XmlTextReader("attrs.xml")
            
            '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

此範例會使用 檔案 attrs.xml ,作為輸入。


<book genre='novel' ISBN='1-861003-78' pubdate='1987'>
</book>

備註

注意

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

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

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

另請參閱

適用於

GetAttribute(String, String)

Source:
XmlTextReader.cs
Source:
XmlTextReader.cs
Source:
XmlTextReader.cs

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

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

參數

localName
String

屬性的本機名稱。

namespaceURI
String

屬性的命名空間 URI。

傳回

指定的屬性值。 如果找不到屬性,會傳回 null。 這個方法不會移動讀取器。

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 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 屬性取得這項資訊。

另請參閱

適用於