XmlAttribute Class
Namespace: System.Xml
Assembly: System.Xml.dll
Assembly: System.Xml.dll
Represents an attribute of an element in the XML document object model (DOM).
Syntax
public class XmlAttribute : XmlNode
Inheritance
Object
→ XmlNode
→ XmlAttribute
Remarks
An XmlAttribute
node is a child of an XmlElement
node. It stores name/value pairs for an element. The Value
property returns the concatenated values of all descendant text nodes. If the attribute has no text nodes, it returns an empty string. If the attribute has text nodes and other node types, it returns the concatenated string values of the text nodes.
The XmlAttribute
class provides properties to access the attribute's name, value, and the element it belongs to. You can create and manipulate attributes using methods of the XmlDocument
class, such as CreateAttribute
and SetAttribute
.
Examples
Creating and Appending an Attribute
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root></root>");
// Create an attribute
XmlAttribute attr = doc.CreateAttribute("myAttribute");
attr.Value = "attributeValue";
// Append the attribute to the root element
doc.DocumentElement.Attributes.Append(attr);
Console.WriteLine(doc.OuterXml);
// Output: <root myAttribute="attributeValue"></root>
Properties
Name | Description |
---|---|
Name |
Gets the qualified name of the node. |
LocalName |
Gets the name of the node without its prefix. |
NamespaceURI |
Gets the Uniform Resource Identifier (URI) of the namespace of this node. |
Prefix |
Gets the namespace prefix of the node. |
Value |
Gets or sets the value of this node. For an XmlAttribute , this is the attribute's value. |
OwnerElement |
Gets the XmlElement that owns this attribute. |
NodeType |
Gets the node type. |
Methods
Name | Description |
---|---|
CloneNode(bool deep) |
Creates a duplicate of this node. If deep is true , it also copies all children of the node. (Overrides XmlNode.CloneNode ) |
GetAttribute(string name) |
Gets the value of the attribute with the specified name. |
RemoveAttribute(string name) |
Removes the attribute with the specified name. |