XmlDocument doc = new XmlDocument(); doc.LoadXml(""); XmlElement newElement = doc.CreateElement("newElement"); XmlElement targetElement = (XmlElement)doc.SelectSingleNode("//target"); targetElement.InsertAfter(newElement, targetElement);
XmlNodeList nodes = doc.SelectNodes("//root/*"); XmlElement firstElement = (XmlElement)nodes[0]; XmlElement secondElement = (XmlElement)nodes[1]; XmlElement thirdElement = (XmlElement)nodes[2]; firstElement.ParentNode.InsertAfter(thirdElement, firstElement); firstElement.ParentNode.InsertAfter(secondElement, firstElement);In this example, all the child elements of the root element are selected using the SelectNodes method. The first, second, and third elements are then located using indexing and cast to XmlElement types. Finally, the InsertAfter method is called on the parent node (the root element) to reorder the elements so that the third element comes before the second element, and the second element comes before the first element.