Mono Class Library: System.Xml.XmlTextReader Overview | Members

System.Xml.XmlTextReader.GetAttribute Method

Returns the value of the attribute with the specified local name and namespace URI.

public override string GetAttribute (string localName, string namespaceURI)

Parameters

localName
A string specifying the local name of the attribute.
namespaceURI
A string specifying the namespace URI of the attribute.
namespaceURI
Documentation for this section has not yet been entered.

Returns

A string containing the value of the specified attribute, or null if the attribute is not found. If localname is null, null is returned.

Remarks

If namespaceURI is null, the local namespace is searched for localName.

This method does not move the reader.

Note:

This method overrides XmlReader.GetAttribute(string).

Example

This example writes the value of the attributes from the following XML fragment to the console:

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

The second attribute value is retrieved using all three overloads of this method.

C# Example

using System;
using System.Xml;

public class Reader {

  public static void Main() {

    string xmlFragment = @"<test xmlns:dt=""urn:datatypes""
                            dt:type=""int""/>";

    NameTable nameTable = new NameTable();
    XmlNamespaceManager xmlNsMan = new 
         XmlNamespaceManager(nameTable);
    XmlParserContext xmlPContext = new
         XmlParserContext(null, xmlNsMan,
                          null, XmlSpace.None);
    XmlTextReader xmlTReader = new
         XmlTextReader(xmlFragment,XmlNodeType.Element,
                       xmlPContext);

    xmlTReader.Read();
    Console.WriteLine( "{0}", xmlTReader.GetAttribute(0) );

    string str1 = xmlTReader.GetAttribute(1);
    string str2 = xmlTReader.GetAttribute("dt:type");
    string str3 = xmlTReader.GetAttribute("type",
                                          "urn:datatypes");
    Console.WriteLine("{0} - {1} - {2}",
                      str1, str2, str3);
  }
}
   

The output is

urn:datatypes

int - int - int

Requirements

Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0