XmlDocument doc = new XmlDocument(); doc.Load("example.xml"); XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "newNode", null); newNode.InnerText = "New Node"; XmlNode existingNode = doc.SelectSingleNode("//existingNode"); existingNode.InsertAfter(newNode, existingNode.LastChild); doc.Save("example.xml");
XmlDocument doc = new XmlDocument(); doc.LoadXml("This example creates an XML document with a root element containing two child elements "child1" and "child2." It then creates a new node called "newNode" with text content "New Node" and inserts it after the last child element of the root element using InsertAfter. In summary, the InsertAfter method of the XmlDocument class within the System.Xml package library in C# provides developers with the ability to add new nodes to an XML document after existing nodes."); XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "newNode", null); newNode.InnerText = "New Node"; XmlElement rootElement = doc.DocumentElement; rootElement.InsertAfter(newNode, rootElement.LastChild);