MSDN - Microsoft Docs

XmlAttribute Class

System.Xml

Represents an attribute of an element.

Syntax


public sealed class XmlAttribute : XmlNode
            

Remarks

An XmlAttribute object represents an attribute of an XmlElement. Attributes are name/value pairs that can be added to an XML element. The XmlAttribute class provides properties to access the attribute's name, value, and other information.

You can create new attributes using the CreateAttribute method of the XmlDocument class. You can then add the created attribute to an element using the SetAttributeNode method.

The XmlAttribute class inherits from XmlNode, so it has many of the same properties and methods as other XML node types, such as Name, Value, and OwnerElement.

Properties

Name Description
AttributeDeclaringType Gets the XmlType object that describes the attribute declaration.
BaseURI Gets the base URI of the current node.
ChildNodes Gets a XmlNodeList containing the child nodes of this node. For XmlAttribute, this node list is empty.
LocalName Gets the local name of the node. For XmlAttribute, this is the attribute name without any namespace prefix.
Name Gets the qualified name of the node. For XmlAttribute, this is the attribute name, including any namespace prefix.
NamespaceURI Gets the System.Xml namespace URI of the node.
NodeType Gets the type of the current node. This property returns XmlNodeType.Attribute.
OwnerDocument Gets the XmlDocument to which this node belongs.
OwnerElement Gets the XmlElement that owns this attribute.
Prefix Gets the namespace prefix of the node.
Value Gets or sets the value of the attribute.

Methods

Name Description
CloneNode(bool deep) Creates a duplicate of this node. If deep is true, recursively clones all the children of the node. For XmlAttribute, the deep parameter has no effect as attributes cannot have child nodes.
GetAttribute(string name) Gets the value of the attribute with the specified name.
GetAttribute(string localName, string namespaceURI) Gets the value of the attribute with the specified local name and namespace URI.
SetAttributeNode(XmlAttribute attribute) Adds the specified XmlAttribute to the element.
RemoveAttributeNode(XmlAttribute attribute) Removes the specified XmlAttribute.

Example

The following C# code demonstrates how to create and use an XmlAttribute:


using System;
using System.Xml;

public class XmlAttributeExample
{
    public static void Main(string[] args)
    {
        XmlDocument doc = new XmlDocument();

        // Create an element
        XmlElement element = doc.CreateElement("Book");

        // Create an attribute
        XmlAttribute attribute = doc.CreateAttribute("category");
        attribute.Value = "programming";

        // Set the attribute on the element
        element.SetAttributeNode(attribute);

        // Append the element to the document
        doc.AppendChild(element);

        // Output the XML
        Console.WriteLine(doc.OuterXml);

        // Accessing attribute properties
        Console.WriteLine($"Attribute Name: {attribute.Name}");
        Console.WriteLine($"Attribute Value: {attribute.Value}");
        Console.WriteLine($"Owner Element: {attribute.OwnerElement.Name}");
    }
}
            

Output of the example


<Book category="programming" />
Attribute Name: category
Attribute Value: programming
Owner Element: Book