Change XML root element name

后端 未结 5 763
星月不相逢
星月不相逢 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;
    
    0 讨论(0)
  • 2021-01-12 03:17

    You can use LINQ to XML to parse the XML string, create a new root and add the child elements and attributes of the original root to the new root:

    XDocument doc = XDocument.Parse("<ItemMasterList>...</ItemMasterList>");
    
    XDocument result = new XDocument(
        new XElement("Masterlist", doc.Root.Attributes(), doc.Root.Nodes()));
    
    0 讨论(0)
  • 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("<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>");
    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)

    <MasterList>
      <ItemMaster>
         <fpartno>xxx</fpartno>
         <frev>000</frev>
         <fac>Default</fac>
      </ItemMaster>
    </MasterList>
    
    0 讨论(0)
  • 2021-01-12 03:19

    I know i am a bit late, but just have to add this answer as no one seems to know about this.

    XDocument doc = XDocument.Parse("<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>");    
    doc.Root.Name = "MasterList";
    

    Which returns the following:

    <MasterList>
      <ItemMaster>
        <fpartno>xxx</fpartno>
        <frev>000</frev>
        <fac>Default</fac>
      </ItemMaster>
    </MasterList>
    
    0 讨论(0)
  • 2021-01-12 03:38

    System.Xml.XmlDocument and the associated classes in that same namespace will prove invaluable to you here.

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(yourString);
    XmlDocument docNew = new XmlDocument();
    XmlElement newRoot = docNew.CreateElement("MasterList");
    docNew.AppendChild(newRoot);
    newRoot.InnerXml = doc.DocumentElement.InnerXml;
    String xml = docNew.OuterXml;
    
    0 讨论(0)
提交回复
热议问题