Inserting XML node at specific position

前端 未结 3 1293
遇见更好的自我
遇见更好的自我 2020-12-11 08:36

I have an XML file and I am loading it in Xmldocument. This document has a node with some child nodes like this


 
   

        
3条回答
  •  一生所求
    2020-12-11 09:10

    you can use XLinq to modify XML document

    Following is an example of xml modification

        String xmlString = ""+""+
        ""+
        "  "+
        " 1"+ //1
        " 2"+ //2
        " 3"+ // 3, I need to insert it
        " 4"+  //4,  I need to insert this second time
        " 5"+
        " 6"+ 
        "  "+
        " "+
        "";
    
        XElement root = XElement.Parse(xmlString);
        var childrens = root.Descendants("children").ToArray();
        var third = childrens[3];
        var fourth = childrens[4];
        third.AddBeforeSelf(new XElement("children"));
        fourth.AddBeforeSelf(new XElement("children"));
    
        var updatedchildren = root.Descendants("children").ToArray();
    

提交回复
热议问题