语言

XmlReader.GetAttribute 方法

定义

在派生类中重写时,获取特性的值。

重载

名称 说明
GetAttribute(String, String)

在派生类中重写时,获取具有指定 LocalName 属性的值和 NamespaceURI。

GetAttribute(Int32)

在派生类中重写时,获取具有指定索引的属性的值。

GetAttribute(String)

在派生类中重写时,获取具有指定 Name属性的值。

GetAttribute(String, String)

Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs

在派生类中重写时,获取具有指定 LocalName 属性的值和 NamespaceURI。

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 则返回。 此方法不会移动读取器。

例外

name 是 null。

在上一个异步操作完成之前调用了一个 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)

Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
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)

Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
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 则返回。

例外

name 是 null。

在上一个异步操作完成之前调用了一个 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")

适用于