XmlElement.GetAttributeNode Method

Definition

Return the specified XmlAttribute.

Overloads

GetAttributeNode(String)

Returns the XmlAttribute with the specified name.

GetAttributeNode(String, String)

Returns the XmlAttribute with the specified local name and namespace URI.

GetAttributeNode(String)

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

Returns the XmlAttribute with the specified name.

C#
public virtual System.Xml.XmlAttribute GetAttributeNode(string name);
C#
public virtual System.Xml.XmlAttribute? GetAttributeNode(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 specified XmlAttribute or null if a matching attribute was not found.

Examples

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

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")){
      XmlAttribute attr = root.GetAttributeNode("genre");
      Console.WriteLine(attr.Value);
   }
  }
}

Applies to

.NET 10 and other versions
Product Versions
.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, 10
.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

GetAttributeNode(String, String)

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

Returns the XmlAttribute with the specified local name and namespace URI.

C#
public virtual System.Xml.XmlAttribute GetAttributeNode(string localName, string namespaceURI);
C#
public virtual System.Xml.XmlAttribute? GetAttributeNode(string localName, string? namespaceURI);

Parameters

localName
String

The local name of the attribute.

namespaceURI
String

The namespace URI of the attribute.

Returns

The specified XmlAttribute or null if a matching attribute was not found.

Applies to

.NET 10 and other versions
Product Versions
.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, 10
.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