XmlElement.GetAttribute Method

Definition

Returns the attribute value for the specified attribute.

Overloads

GetAttribute(String)

Returns the value for the attribute with the specified name.

GetAttribute(String, String)

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

GetAttribute(String)

Source:
XmlElement.cs
Source:
XmlElement.cs
Source:
XmlElement.cs

Returns the value for the attribute with the specified name.

public virtual string GetAttribute (string name);

Parameters

name
String

The name of the attribute to retrieve. This is a qualified name. It is matched against the Name property of the matching node.

Returns

The value of the specified attribute. An empty string is returned if a matching attribute is not found or if the attribute does not have a specified or default value.

Examples

The following example checks to see if the element has the specified attribute.

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

Applies to

.NET 9 및 기타 버전
제품 버전
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

GetAttribute(String, String)

Source:
XmlElement.cs
Source:
XmlElement.cs
Source:
XmlElement.cs

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

public virtual string GetAttribute (string localName, string namespaceURI);
public virtual string GetAttribute (string localName, string? namespaceURI);

Parameters

localName
String

The local name of the attribute to retrieve.

namespaceURI
String

The namespace URI of the attribute to retrieve.

Returns

The value of the specified attribute. An empty string is returned if a matching attribute is not found or if the attribute does not have a specified or default value.

Applies to

.NET 9 및 기타 버전
제품 버전
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0