This documentation is archived and is not being maintained.

XmlElement.GetAttribute Method

Returns the attribute value for the specified attribute.

Overload List

Returns the value for the attribute with the specified name.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function GetAttribute(String) As String
[C#] public virtual string GetAttribute(string);
[C++] public: virtual String* GetAttribute(String*);
[JScript] public function GetAttribute(String) : String;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function GetAttribute(String, String) As String
[C#] public virtual string GetAttribute(string, string);
[C++] public: virtual String* GetAttribute(String*, String*);
[JScript] public function GetAttribute(String, String) : String;

Example

[Visual Basic, C#, C++] The following example checks to see if the element has the specified attribute.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetAttribute. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
                "<title>Pride And Prejudice</title>" & _
                "</book>")      

    Dim root as XmlElement = doc.DocumentElement

    ' Check to see if the element has a genre attribute.
    if (root.HasAttribute("genre"))
     Dim genre as String = root.GetAttribute("genre")
     Console.WriteLine(genre)
    end if
       
    Console.WriteLine("Display the modified XML...")
    Console.WriteLine(doc.InnerXml)

  end sub
end class

[C#] 
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    // Check to see if the element has a genre attribute.
    if (root.HasAttribute("genre")){
      String genre = root.GetAttribute("genre");
      Console.WriteLine(genre);
   }

  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{

    XmlDocument* doc = new XmlDocument();
    doc->LoadXml(S"<book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book>");

    XmlElement* root = doc->DocumentElement;

    // Check to see if the element has a genre attribute.
    if (root->HasAttribute(S"genre")){
      String* genre = root->GetAttribute(S"genre");
      Console::WriteLine(genre);
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

XmlElement Class | XmlElement Members | System.Xml Namespace

Show: