XmlReader.GetAttribute 方法

定義

當在衍生類別中覆寫時,會得到屬性的值。

多載

名稱 Description
GetAttribute(String, String)

當在派生類別中覆寫時,會得到具有指定 LocalNameNamespaceURI的屬性值。

GetAttribute(Int32)

當在派生類別中覆寫時,會得到具有指定索引的屬性值。

GetAttribute(String)

當在衍生類別中覆寫時,會得到具有指定 Name屬性的值。

GetAttribute(String, String)

來源:
XmlReader.cs
來源:
XmlReader.cs
來源:
XmlReader.cs
來源:
XmlReader.cs
來源:
XmlReader.cs

當在派生類別中覆寫時,會得到具有指定 LocalNameNamespaceURI的屬性值。

public:
 abstract System::String ^ GetAttribute(System::String ^ name, System::String ^ namespaceURI);
public abstract string GetAttribute(string name, string namespaceURI);
public abstract string? GetAttribute(string name, string? namespaceURI);
abstract member GetAttribute : string * string -> string
Public MustOverride Function GetAttribute (name As String, namespaceURI As String) As String

參數

name
String

該屬性的當地名稱。

namespaceURI
String

屬性的命名空間 URI。

傳回

指定屬性的值。 若未找到該屬性,則 null 返回。 此方法不會移動讀者。

例外狀況

namenull

在先前非同步操作結束前,會呼叫一個 XmlReader 方法。 此時, InvalidOperationException 會拋出「非同步操作已進行中」的訊息。

備註

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

適用於

GetAttribute(Int32)

來源:
XmlReader.cs
來源:
XmlReader.cs
來源:
XmlReader.cs
來源:
XmlReader.cs
來源:
XmlReader.cs

當在派生類別中覆寫時,會得到具有指定索引的屬性值。

public:
 abstract System::String ^ GetAttribute(int i);
public abstract string GetAttribute(int i);
abstract member GetAttribute : int -> string
Public MustOverride Function GetAttribute (i As Integer) As String

參數

i
Int32

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

傳回

指定屬性的值。 此方法不會移動讀者。

例外狀況

i 超出射程範圍。 它必須是非負值,且小於屬性集合的大小。

在先前非同步操作結束前,會呼叫一個 XmlReader 方法。 此時, InvalidOperationException 會拋出「非同步操作已進行中」的訊息。

範例

以下範例取第三個屬性的值。

reader.ReadToFollowing("book");
string isbn = reader.GetAttribute(2);
reader.ReadToFollowing("book")
Dim isbn As String = reader.GetAttribute(2)

適用於

GetAttribute(String)

來源:
XmlReader.cs
來源:
XmlReader.cs
來源:
XmlReader.cs
來源:
XmlReader.cs
來源:
XmlReader.cs

當在衍生類別中覆寫時,會得到具有指定 Name屬性的值。

public:
 abstract System::String ^ GetAttribute(System::String ^ name);
public abstract string GetAttribute(string name);
public abstract string? GetAttribute(string name);
abstract member GetAttribute : string -> string
Public MustOverride Function GetAttribute (name As String) As String

參數

name
String

屬性的限定名稱。

傳回

指定屬性的值。 若未找到該屬性,則 null 返回。 如果屬性存在但值為空,則 Empty 回傳。

例外狀況

namenull

在先前非同步操作結束前,會呼叫一個 XmlReader 方法。 此時, InvalidOperationException 會拋出「非同步操作已進行中」的訊息。

範例

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

reader.ReadToFollowing("book");
string isbn = reader.GetAttribute("ISBN");
Console.WriteLine("The ISBN value: " + isbn);
reader.ReadToFollowing("book")
Dim isbn As String = reader.GetAttribute("ISBN")
Console.WriteLine("The ISBN value: " + isbn)

備註

此方法不會移動讀者。

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

適用於