共用方式為


XmlNodeReader.GetAttribute 方法

定義

取得屬性的值。

多載

名稱 Description
GetAttribute(Int32)

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

GetAttribute(String)

取得指定名稱屬性的值。

GetAttribute(String, String)

取得具有指定本地名稱與命名空間 URI 的屬性值。

GetAttribute(Int32)

來源:
XmlNodeReader.cs
來源:
XmlNodeReader.cs
來源:
XmlNodeReader.cs
來源:
XmlNodeReader.cs
來源:
XmlNodeReader.cs

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

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

傳回

指定屬性的值。

例外狀況

attributeIndex 小於 0 或大於或等於 AttributeCount

備註

建議做法是使用XmlReaderSettings類別和Create方法來建立XmlReader實例。

此方法不會移動讀者。

適用於

GetAttribute(String)

來源:
XmlNodeReader.cs
來源:
XmlNodeReader.cs
來源:
XmlNodeReader.cs
來源:
XmlNodeReader.cs
來源:
XmlNodeReader.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

屬性的限定名稱。

傳回

指定屬性的值。 若未找到該屬性,則 null 返回。

範例

以下範例取得 ISBN 屬性的值。

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

備註

備註

為了利用新功能,建議的做法是使用XmlReaderSettings該類別和該Create方法來建立XmlReader實例。 欲了解更多資訊,請參閱參考頁面的 XmlReader 備註區。

此方法不會移動讀者。

若讀取器位於節點 DocumentType 上,此方法可用來取得 PUBLIC 與 SYSTEM 字面,例如, reader.GetAttribute("PUBLIC")

適用於

GetAttribute(String, String)

來源:
XmlNodeReader.cs
來源:
XmlNodeReader.cs
來源:
XmlNodeReader.cs
來源:
XmlNodeReader.cs
來源:
XmlNodeReader.cs

取得具有指定本地名稱與命名空間 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。

傳回

指定屬性的值。 若未找到該屬性,則 null 返回。

備註

備註

為了利用新功能,建議的做法是使用XmlReaderSettings該類別和該Create方法來建立XmlReader實例。 欲了解更多資訊,請參閱參考頁面的 XmlReader 備註區。

以下 XML 包含特定命名空間中的一個屬性:

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

你可以用一個參數(前綴和本地名稱)或兩個參數(本地名稱和命名空間 URI)來查找該 dt:type 屬性:

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 房產取得這些資訊。

適用於