XmlElement Class
Namespace: System.Xml
Assembly: System.Xml.dll
Represents an element node in the XML document object model (DOM).
Syntax
Remarks
An XmlElement
is a node that represents an XML element. It can contain other nodes, such as child elements, text, comments, and processing instructions. XmlElement
implements the IXmlNode interface.
You can create an XmlElement
using the CreateElement method of the XmlDocument
class.
Properties
-
BaseURI : stringget;
Gets the base URI of the current node.
-
ChildNodes : XmlNodeListget;
Gets a XmlNodeList that represents all the children of the current node.
-
FirstChild : XmlNodeget;
Gets the first child of this node. If there is no such node, null is returned.
-
HasAttributes : boolget;
Gets a value indicating whether the current node has any attributes.
-
IsEmpty : boolget;
Gets a value indicating whether the element is an empty element (e.g.,
<element/>
). -
IsReadOnly : boolget;
Determines whether the specified node is read-only.
-
LastChild : XmlNodeget;
Gets the last child of this node. If there is no such node, null is returned.
-
LocalName : stringget;
Gets the local name of the current node. For
XmlElement
, this is the tag name of the element. -
Name : stringget;
Gets the qualified name of the node.
-
NextSibling : XmlNodeget;
Gets the node immediately following this node. If there is no immediately following node, null is returned.
-
NodeType : XmlNodeTypeget;
Gets the node type.
-
OwnerDocument : XmlDocumentget;
Gets the
XmlDocument
to which this node belongs. -
ParentNode : XmlNodeget;
Gets the parent of this node.
-
Prefix : stringget;
Gets or sets the namespace prefix of the node.
-
PreviousSibling : XmlNodeget;
Gets the node immediately preceding this node. If there is no immediately preceding node, null is returned.
-
SchemaInfo : IXmlSchemaInfoget;
Gets the XmlSchema information for the node.
-
Tags : XmlAttributeCollectionget;
Gets the collection of attributes of this element.
-
Value : stringget;
Gets or sets the value of the current node.
Methods
-
AppendChild : XmlNodepublic XmlNode AppendChild(XmlNode newChild);
Appends the specified node to the end of the list of child nodes of this node.
-
Clone : XmlNodepublic XmlNode Clone();
Creates a duplicate of this node.
-
GetAttribute : stringpublic string GetAttribute(string name);
Gets the value of the attribute with the specified name.
-
GetElementsByTagName : XmlNodeListpublic XmlNodeList GetElementsByTagName(string name);
Retrieves a list of all descendant elements with the given tag name.
-
InsertAfter : XmlNodepublic XmlNode InsertAfter(XmlNode newChild, XmlNode refChild);
Inserts the specified node immediately after the specified node.
-
InsertBefore : XmlNodepublic XmlNode InsertBefore(XmlNode newChild, XmlNode refChild);
Inserts the specified node immediately before the specified node.
-
RemoveAll : voidpublic void RemoveAll();
Removes all attributes and child nodes from this element.
-
RemoveAttribute : voidpublic void RemoveAttribute(string name);
Removes the attribute with the specified name.
-
RemoveChild : XmlNodepublic XmlNode RemoveChild(XmlNode oldChild);
Removes the specified node from the list of child nodes and returns the removed node.
-
SetAttribute : voidpublic void SetAttribute(string name, string value);
Sets the value of the attribute with the specified name.
-
Supports : boolpublic bool Supports(string feature, string version);
Determines whether the DOM implementation supports a particular feature.
Example
Creating and Manipulating an XmlElement
using System;
using System.Xml;
public class XmlElementExample
{
public static void Main(string[] args)
{
// Create an XmlDocument
XmlDocument doc = new XmlDocument();
// Create an XmlElement
XmlElement rootElement = doc.CreateElement("bookstore");
doc.AppendChild(rootElement);
// Create another XmlElement
XmlElement bookElement = doc.CreateElement("book");
rootElement.AppendChild(bookElement);
// Set an attribute
bookElement.SetAttribute("category", "programming");
// Create child elements
XmlElement titleElement = doc.CreateElement("title");
titleElement.InnerText = ".NET Programming";
bookElement.AppendChild(titleElement);
XmlElement authorElement = doc.CreateElement("author");
authorElement.InnerText = "John Doe";
bookElement.AppendChild(authorElement);
// Get an attribute
string category = bookElement.GetAttribute("category");
Console.WriteLine($"Book category: {category}"); // Output: Book category: programming
// Get child elements by tag name
XmlNodeList titleNodes = bookElement.GetElementsByTagName("title");
if (titleNodes.Count > 0)
{
Console.WriteLine($"Book title: {titleNodes[0].InnerText}"); // Output: Book title: .NET Programming
}
// Output the XML
Console.WriteLine("\nXML Structure:");
Console.WriteLine(doc.OuterXml);
}
}