Change XML root element name

后端 未结 5 800
星月不相逢
星月不相逢 2021-01-12 02:40

I have XML stored in string variable:

xxx000Defaul         


        
5条回答
  •  爱一瞬间的悲伤
    2021-01-12 03:15

    As pointed by Will A, we can do it that way but for case where InnerXml equals the OuterXml the following solution will work out:

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

提交回复
热议问题