// Create a new Xml doc object with root node as "NewRootNode" and // copy the inner content from old doc object using the LastChild. XmlDocument doc = new XmlDocument("FileName"); XmlElement newRoot = docNew.CreateElement("NewRootNode");XmlDocument docNew = new XmlDocument();docNew.AppendChild(newRoot); // The below line solves the InnerXml equals the OuterXml Problem
newRoot.InnerXml = oldDoc.LastChild.InnerXml; string xmlText = docNew.OuterXml;
// docNew contains the XML Document with replaced Root Node.
↧
Rename Root Node in XML
↧