XmlReader.GetAttribute Method

Definition

When overridden in a derived class, gets the value of an attribute.

Overloads

GetAttribute(String, String)

When overridden in a derived class, gets the value of the attribute with the specified LocalName and NamespaceURI.

GetAttribute(Int32)

When overridden in a derived class, gets the value of the attribute with the specified index.

GetAttribute(String)

When overridden in a derived class, gets the value of the attribute with the specified Name.

GetAttribute(String, String)

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

Parameters

name
String

The local name of the attribute.

namespaceURI
String

The namespace URI of the attribute.

Returns

The value of the specified attribute. If the attribute is not found or the value is String.Empty, null is returned. This method does not move the reader.

Exceptions

name is null.

An XmlReader method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Remarks

The following XML contains an attribute in a specific namespace:

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

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");

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/);

You can also get this information using the Prefix property.

Applies to

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

Parameters

i
Int32

The index of the attribute. The index is zero-based. (The first attribute has index 0.)

Returns

The value of the specified attribute. This method does not move the reader.

Exceptions

i is out of range. It must be non-negative and less than the size of the attribute collection.

An XmlReader method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Examples

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)

Applies to

GetAttribute(String)

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);
public abstract string? GetAttribute (string name);
abstract member GetAttribute : string -> string
Public MustOverride Function GetAttribute (name As String) As String

Parameters

name
String

The qualified name of the attribute.

Returns

The value of the specified attribute. If the attribute is not found or the value is String.Empty, null is returned.

Exceptions

name is null.

An XmlReader method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Examples

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)

Remarks

This method does not move the reader.

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

Applies to