System.Xml
Represents an XML element.
The XmlElement
class is derived from the XmlNode
class and represents an XML element. It provides properties and methods for accessing and manipulating the element's name, attributes, and child nodes.
System.Object
System.Xml.XmlNode
System.Xml.XmlElement
public class XmlElement : XmlNode
The XmlElement
class does not expose public constructors directly. Instances are typically created by methods of the XmlDocument
class, such as CreateElement
.
Gets an XmlAttributeCollection object that contains the attributes of this element and all the child elements.
Gets the base Uniform Resource Identifier (URI) of the current node.
Gets a XmlNodeList containing the children of this node. If there are no children, this is an empty XmlNodeList.
Gets the first child of this node. If there is no such node, it returns null
.
Gets a value indicating whether this node has any attributes.
Gets a value indicating whether this node is read-only. true
if read-only; otherwise, false
.
Indicates whether the current node is equal to the node specified by the ICloneable.Clone() parameter.
Gets the last child of this node. If there is no such node, it returns null
.
Gets the local name of the current node. For XmlElement
, this is the tag name of the element.
Gets the qualified name of the node. For XmlElement
, this is the element name.
Gets the node immediately following this node. If there is no such node, it returns null
.
Gets the type of the current node. For XmlElement
, this is XmlNodeType.Element
.
Gets the XmlDocument to which this node belongs.
Gets the parent of this node (the XmlDocument or XmlElement that contains this node). If this node is the root of the document, it returns null
.
Gets or sets the namespace prefix of this node.
Gets the node immediately preceding this node. If there is no such node, it returns null
.
Gets schema information for the current node.
Gets or sets the value of the current node. For an XmlElement
, this is the concatenated value of all its child nodes.
Adds the specified node to the end of the list of children of this node. Returns the added node.
Creates a copy of this node. Returns the cloned node.
Gets the value of the attribute with the specified name.
Returns a XmlNodeList containing all the descendant elements with the given tag name, in document order.
Determines whether the element has an attribute with the specified name.
Inserts the specified node immediately after the specified XmlNode.
Inserts the specified node immediately before the node for which it is a child.
Normalizes the node by selecting the document order nodes into the subtree and removing empty text nodes.
Adds the specified node to the beginning of the list of children of this node. Returns the added node.
Removes all attributes from the element.
Removes the attribute with the specified name.
Removes the specified attribute.
Removes the specified child node. Returns the node that was removed.
Replaces the child node that is specified by the XmlNode parameter with the new node specified by the XmlNode parameter.
Sets the value of the attribute with the specified name. If the attribute is not found, it is created.
Adds the specified attribute.
using System;
using System.Xml;
public class Example
{
public static void Main(string[] args)
{
// Create a new XML document
XmlDocument doc = new XmlDocument();
// Create an element
XmlElement elem = doc.CreateElement("Book");
elem.SetAttribute("ISBN", "0-7356-0535-9");
// Create child elements and text nodes
XmlElement title = doc.CreateElement("Title");
title.InnerText = "The C++ Programming Language";
elem.AppendChild(title);
XmlElement author = doc.CreateElement("Author");
author.InnerText = "Bjarne Stroustrup";
elem.AppendChild(author);
// Append the element to the document
doc.AppendChild(elem);
// Save the document
Console.WriteLine(doc.OuterXml);
}
}
<Book ISBN="0-7356-0535-9"><Title>The C++ Programming Language</Title><Author>Bjarne Stroustrup</Author></Book>