XmlElement Class

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.

Inheritance

System.Object
System.Xml.XmlNode
System.Xml.XmlElement

Syntax

public class XmlElement : XmlNode

Constructors

The XmlElement class does not expose public constructors directly. Instances are typically created by methods of the XmlDocument class, such as CreateElement.

Properties

Methods

Example

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);
    }
}

Output

<Book ISBN="0-7356-0535-9"><Title>The C++ Programming Language</Title><Author>Bjarne Stroustrup</Author></Book>

See Also

System.Xml Namespace
XmlNode Class
XmlDocument Class