Change XML root element name

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

I have XML stored in string variable:

xxx000Defaul         


        
5条回答
  •  抹茶落季
    2021-01-12 03:18

    Using the XmlDocument way, you can do this as follows (and keep the tree intact):

    XmlDocument oldDoc = new XmlDocument();
    oldDoc.LoadXml("xxx000Default");
    XmlNode node = oldDoc.SelectSingleNode("ItemMasterList");
    
    XmlDocument newDoc = new XmlDocument();
    XmlElement ele = newDoc.CreateElement("MasterList");
    ele.InnerXml = node.InnerXml;
    

    If you now use ele.OuterXml is will return: (you you just need the string, otherwise use XmlDocument.AppendChild(ele) and you will be able to use the XmlDocument object some more)

    
      
         xxx
         000
         Default
      
    
    

提交回复
热议问题