XDocument xmlDoc = XDocument.Load("example.xml"); xmlDoc.Element("root").Add(new XElement("newElement", "newElementValue"));
XDocument xmlDoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("root", new XElement("child1", "child1Value"), new XElement("child2", "child2Value") ) );In this example, we create a new XML document using the XDocument constructor and add two child elements to the root element. Package library: System.Xml.Linq.