XmlReader.GetAttribute XmlReader.GetAttribute XmlReader.GetAttribute XmlReader.GetAttribute Method

定义

当在派生类中被重写时,获取属性的值。When overridden in a derived class, gets the value of an attribute.

重载

GetAttribute(String, String) GetAttribute(String, String) GetAttribute(String, String) GetAttribute(String, String)

当在派生类中被重写时,获取具有指定 LocalNameNamespaceURI 的属性的值。When overridden in a derived class, gets the value of the attribute with the specified LocalName and NamespaceURI.

GetAttribute(Int32) GetAttribute(Int32) GetAttribute(Int32) GetAttribute(Int32)

当在派生类中被重写时,获取具有指定索引的属性的值。When overridden in a derived class, gets the value of the attribute with the specified index.

GetAttribute(String) GetAttribute(String) GetAttribute(String) GetAttribute(String)

当在派生类中被重写时,获取具有指定 Name 的属性的值。When overridden in a derived class, gets the value of the attribute with the specified Name.

GetAttribute(String, String) GetAttribute(String, String) GetAttribute(String, String) GetAttribute(String, String)

当在派生类中被重写时,获取具有指定 LocalNameNamespaceURI 的属性的值。When overridden in a derived class, gets the value of the attribute with the specified LocalName and NamespaceURI.

public:
 abstract System::String ^ GetAttribute(System::String ^ name, System::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 String String String

属性的本地名称。The local name of the attribute.

namespaceURI
String String String String

属性的命名空间 URI。The namespace URI of the attribute.

返回

指定的属性的值。The value of the specified attribute. 如果找不到该属性,或者值为 String.Empty,则返回 nullIf the attribute is not found or the value is String.Empty, null is returned. 此方法不移动读取器。This method does not move the reader.

异常

在上一次异步操作完成之前调用了 XmlReader 方法。An XmlReader method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

注解

以下 XML 包含特定命名空间中的属性:The following XML contains an attribute in a specific namespace:

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

可以使用一个参数dt:type (前缀和本地名称)或两个参数(本地名称和命名空间 URI)来查找属性:You can lookup the dt:type attribute using one argument (prefix and local name) or two arguments (local name and namespace URI):

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

若要查找xmlns:dt属性,请使用以下参数之一:To lookup the xmlns:dt attribute, use one of the following arguments:

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

你还可以使用Prefix属性获取此信息。You can also get this information using the Prefix property.

GetAttribute(Int32) GetAttribute(Int32) GetAttribute(Int32) GetAttribute(Int32)

当在派生类中被重写时,获取具有指定索引的属性的值。When overridden in a derived class, gets the value of the attribute with the specified index.

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 Int32 Int32 Int32

属性的索引。The index of the attribute. 索引是从零开始的。The index is zero-based. (第一个属性的索引为 0。)(The first attribute has index 0.)

返回

指定的属性的值。The value of the specified attribute. 此方法不移动读取器。This method does not move the reader.

异常

i 超出范围。i is out of range. 它必须是非负数且小于特性集合的大小。It must be non-negative and less than the size of the attribute collection.

在上一次异步操作完成之前调用了 XmlReader 方法。An XmlReader method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

示例

下面的示例获取第三个属性的值。The following example gets the value of the third attribute.

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

GetAttribute(String) GetAttribute(String) GetAttribute(String) GetAttribute(String)

当在派生类中被重写时,获取具有指定 Name 的属性的值。When overridden in a derived class, gets the value of the attribute with the specified Name.

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

参数

name
String String String String

属性的限定名称。The qualified name of the attribute.

返回

指定的属性的值。The value of the specified attribute. 如果找不到该属性,或者值为 String.Empty,则返回 nullIf the attribute is not found or the value is String.Empty, null is returned.

异常

在上一次异步操作完成之前调用了 XmlReader 方法。An XmlReader method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

示例

下面的示例获取 ISBN 特性的值。The following example gets the value of the ISBN attribute.

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)

注解

此方法不移动读取器。This method does not move the reader.

如果读取器定位在DocumentType节点上,则此方法可用于获取公共和系统文本,例如reader.GetAttribute("PUBLIC")If the reader is positioned on a DocumentType node, this method can be used to get the PUBLIC and SYSTEM literals, for example, reader.GetAttribute("PUBLIC")

适用于