using System.Xml; // load existing XML document XmlDocument doc = new XmlDocument(); doc.Load("existing_doc.xml"); // find parent element to add new child XmlElement parent = doc.SelectSingleNode("//parent") as XmlElement; // create new child element XmlElement child = doc.CreateElement("child"); child.InnerText = "New child content"; // append child to parent element parent.AppendChild(child); // save modified document doc.Save("modified_doc.xml");
using System.Xml; // load existing HTML document XmlDocument doc = new XmlDocument(); doc.LoadHtml("This example loads an existing HTML document, finds a parent element, creates a new element with attributes, appends it to the parent element, and saves the modified document. Package library: System.Xml.My Website Welcome!
"); // find parent element to add new child XmlElement parent = doc.SelectSingleNode("//head") as XmlElement; // create new child element XmlElement newElement = doc.CreateElement("meta"); newElement.SetAttribute("name", "description"); newElement.SetAttribute("content", "My website description"); // append new element to parent parent.AppendChild(newElement); // save modified document doc.Save("modified_html.html");