Overview
Namespace: System.Xml
Assembly: System.Xml.dll
Use XmlElement to work with element nodes, manage attributes, and interact with the element's text content.
Example
Creating an XmlDocument, adding an XmlElement, and manipulating attributes.
// Using System.Xml;
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("books");
doc.AppendChild(root);
XmlElement book = doc.CreateElement("book");
book.SetAttribute("id", "b1");
book.SetAttribute("genre", "fantasy");
XmlElement title = doc.CreateElement("title");
title.InnerText = "The Hobbit";
book.AppendChild(title);
XmlElement author = doc.CreateElement("author");
author.InnerText = "J.R.R. Tolkien";
book.AppendChild(author);
root.AppendChild(book);
Console.WriteLine(doc.OuterXml);
Output:
<books>
<book id="b1" genre="fantasy">
<title>The Hobbit</title>
<author>J.R.R. Tolkien</author>
</book>
</books>