0

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

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

If you don't need the ID-attribute and no other attributes you can save a lot of work. What I can see of the ID-attribute is that it is redundant. You can get that id by logic. And if you create a nice function for "RenameNode()" that covers the attribute transfer and all childs I think many people would be glad if you shared it.
@baaroz if you like my answear please consider to accept it when your're done :)
sure,I am working on it right now and as soon as I finish, I will publish it here and then mark your answer.
0

Sorry misunderstood.... Thinking about it.

By the looks of it you cant change a nodes name. You could try making a new XML file and give them different names there or creating new nodes.

OLD *

Use this in the class where you want to do it:

Private Shared ReadOnly XMLFile As String = "LinkToYourXML"
Dim mappingDataXml As System.Xml.XmlDocument = New System.Xml.XmlDocument mappingDataXml.Load(XMLFile)

For Each node As System.Xml.XmlNode In mappingDataXml.SelectNodes("/genral/mynode/first")

node.SelectSongleNode("nodechild-first").InnerText = "The text you want to have in there."

Next

Hope this could be of any help :)

OLD *

Comments

0

OK This is my solution for the problem,im my xml there isn't same node name twice(so every one sholud make his own changes,but the main idea is the same

  Sub btnc_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnc.Click



                Dim xmldoc As XmlDocument = New XmlDocument()
                xmldoc.Load(Server.MapPath("yuor xml path"))



                Dim nodee As XmlNodeList = xmldoc.GetElementsByTagName(tempstr)//path to node

                ' parent element of the element we want to replace
                Dim parentElement As XmlNode = nodee(0).ParentNode


                ' element we want to replace
                Dim oldXmlNode As XmlNode = nodee(0)

                ' new element
                Dim newXmlElement As XmlElement = xmldoc.CreateElement(txtdes.Value)

                Dim temp As XmlAttribute = oldXmlNode.Attributes("id")//adding attirbute old->new


                newXmlElement.Attributes.Append(temp)


                Dim node As XmlNode = oldXmlNode.FirstChild node

                buildtree(newXmlElement, xmldoc, node)//copy first child to new 

                node = node.NextSibling /taking next Sibling 
                While Not IsNothing(node)//while node has brothers

                    buildtree(newXmlElement, xmldoc, node)/copy Sibling old->new
                    node = node.NextSibling //next Sibling 

                End While


                parentElement.ReplaceChild(newXmlElement, oldXmlNode)// making the switch




                xmldoc.Save(Server.MapPath("path"))




            End Sub


            Sub buildtree(ByVal newnode As XmlNode, ByVal xmldoc As XmlDocument, ByVal oldxmlnode As XmlNode)

                newnode.AppendChild(xmldoc.ImportNode(oldxmlnode, True))




            End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.