XmlAttribute

Namespace: System.Xml
Assembly: System.Xml.dll

Summary

Represents an attribute of an XmlElement.

Inheritance

Object
XmlNode
XmlAttribute

Syntax

public class XmlAttribute : XmlNode

Remarks

An attribute is a name-value pair that is associated with an XmlElement. Attributes are stored as members of the XmlAttribute class. An XmlAttribute object can have child nodes, but these child nodes must be of type XmlAttribute (which is not allowed in the W3C DOM specification) or XmlEntityReference. The value of an attribute is stored as a string in the Value property.

To create a new attribute, use the CreateAttribute method of the XmlDocument class.

Constructors

XmlAttribute(string prefix, string localName, string namespaceURI, XmlDocument doc)

public XmlAttribute(string prefix, string localName, string namespaceURI, XmlDocument doc);

Initializes a new instance of the XmlAttribute class with the specified prefix, local name, namespace URI, and document.

Parameters

Name Type Description
prefix string The prefix of the attribute.
localName string The local name of the attribute.
namespaceURI string The namespace URI of the attribute.
doc XmlDocument The XmlDocument to which the attribute belongs.

XmlAttribute(string qualifiedName, string namespaceURI, XmlDocument doc)

public XmlAttribute(string qualifiedName, string namespaceURI, XmlDocument doc);

Initializes a new instance of the XmlAttribute class with the specified qualified name, namespace URI, and document.

Parameters

Name Type Description
qualifiedName string The qualified name of the attribute.
namespaceURI string The namespace URI of the attribute.
doc XmlDocument The XmlDocument to which the attribute belongs.

Properties

Name

public override string Name { get; }

Gets the qualified name of the node.

Introduced in .NET Framework 1.0.

LocalName

public override string LocalName { get; }

Gets the local name of the node without the prefix.

Introduced in .NET Framework 1.0.

Value

public override string Value { get; set; }

Gets or sets the value of this node.

For XmlAttribute, this property returns the attribute value as a string. Setting this property replaces the attribute's value and its child nodes with the new string.

Introduced in .NET Framework 1.0.

Prefix

public override string Prefix { get; }

Gets the namespace prefix of the node.

Introduced in .NET Framework 1.0.

NamespaceURI

public override string NamespaceURI { get; }

Gets the Uniform Resource Identifier (URI) of the namespace that the node belongs to.

Introduced in .NET Framework 1.0.

OwnerElement

public XmlElement OwnerElement { get; }

Gets the XmlElement that owns this attribute.

This property is obsolete. For an alternative, see ParentNode.

Introduced in .NET Framework 1.0.

IsReadOnly

public override bool IsReadOnly { get; }

Returns true if this node is read-only.

Introduced in .NET Framework 2.0.

Methods

SetAttributeNode(XmlAttribute newAttr)

public virtual XmlAttribute SetAttributeNode(XmlAttribute newAttr);

Adds an XmlAttribute to the element.

Parameters

Name Type Description
newAttr XmlAttribute The XmlAttribute to add.

Returns

XmlAttribute

The XmlAttribute that was added.

Introduced in .NET Framework 1.0.

RemoveAttributeNode(XmlAttribute oldAttr)

public virtual XmlAttribute RemoveAttributeNode(XmlAttribute oldAttr);

Removes the specified attribute node from the element.

Parameters

Name Type Description
oldAttr XmlAttribute The XmlAttribute to remove.

Returns

XmlAttribute

The XmlAttribute that was removed.

Introduced in .NET Framework 1.0.

CloneNode(bool deep)

public override XmlNode CloneNode(bool deep);

Creates a copy of this node. The deepness of the copy is determined by the deep parameter.

Parameters

Name Type Description
deep bool true to recursively clone the subtree under the specified node; otherwise, false.

Returns

XmlNode

The cloned node.

Introduced in .NET Framework 1.0.

Examples

The following example creates an XML document and adds an attribute to an element.


var doc = new System.Xml.XmlDocument();
var root = doc.CreateElement("root");
doc.AppendChild(root);

// Create an attribute
var attribute = doc.CreateAttribute("id");
attribute.Value = "myId123";

// Append the attribute to the element
root.Attributes.Append(attribute);

// You can also set an attribute directly using SetAttribute
root.SetAttribute("version", "1.0");

Console.WriteLine(doc.OuterXml);
// Output: <root id="myId123" version="1.0" />