How to add a new node to an existing xml? using c#

40 Views Asked by At

I am trying to add a new node to an existing xmldocument as a root node, this is the code I have:

public XmlDocument FunctionG()
    {
        XmlDocument xmlResponse = ExecuteGet();
        XmlDocument xmlOperacion = new XmlDocument();
        XmlNode aux;
        
        XmlNodeList nodeListCustom1 = xmlResponse.SelectNodes("/nodes/node/children/node[@ciType='Mabe_Operation']");
        
        aux = nodeListCustom1[0];
       
        XmlNodeList children2 = aux.SelectNodes("dimension");
        aux.RemoveChild(children2[0]);
        XmlNodeList children3 = aux.SelectNodes("dimension");
        aux.RemoveChild(children3[0]);
        XmlNodeList children4 = aux.SelectNodes("dimension");
        aux.RemoveChild(children4[0]);

        aux = nodeListCustom1[0];
        XmlNodeList children = aux.SelectNodes("children");

        aux.RemoveChild(children[0]);

       
        xmlOperacion.LoadXml(aux.OuterXml);
        
      
        XmlNode root = xmlOperacion.DocumentElement;
        XmlNode elem = xmlOperacion.CreateNode(XmlNodeType.Element,"nodes","");
        root.InsertAfter(elem, root.FirstChild);


        return xmlOperacion;

    }

The return returns the following:

<node>
<nodes/>
</node>

But I need it this way:

<nodes>
<node></node>
</nodes>

How can I solve that?

0

There are 0 best solutions below