Search code examples
asp.netxmlxmldocument

change node name in XML using XmlDocument


I have this xml

<genral>
 <mynode id="1">
    <first id="1.1">
          <nodechild-first id="1.1.1"></nodechild-first>
          <nodechild-seconed id="1.1.2"></nodechild-seconed>
    </first>
    </mynode>
</genral>

I need to rename one of the nodes name,for example change the name of <first> to <f> or the <nodechild-first> to <c-f>

How can I do it using asp.net(XmlDocument) .

the values of destination name node and the new name node, is represented by two string variables.

Thanks for any help


Solution

  • You can't just change name of a allready existing node. What you need to do is.

    1. Create a new node with the desired name.
    2. Set all attributes that existed on the original node to the new node.
    3. Append the new node to the original document or where you want it.
    4. Select all childs in the previous node and append them to the new node.
    5. Remove the old node.

    I should also tell you that you can't rename the root node, if you want to do that you either need to switch make 3. after 5. or you need to insert it into a new XMLDocument because it won't allow you to have two root nodes.

    Regards