Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to rename elements in a xml file

How to rename elements in a xml file

Scheduled Pinned Locked Moved C#
xmltutorial
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Phrone
    wrote on last edited by
    #1

    I have a xml file that looks something like this something something else some text something more bla bla more headlines some simple text... (1) list I've tried to rename some elements using XmlDocument() but with no success. So if anyone can tell me how it is done, I would be glad. Elements to rename: niv1, niv2 and niv3 And ofcourse, save the file with changes.

    A E 2 Replies Last reply
    0
    • P Phrone

      I have a xml file that looks something like this something something else some text something more bla bla more headlines some simple text... (1) list I've tried to rename some elements using XmlDocument() but with no success. So if anyone can tell me how it is done, I would be glad. Elements to rename: niv1, niv2 and niv3 And ofcourse, save the file with changes.

      A Offline
      A Offline
      Affan Toor
      wrote on last edited by
      #2

      Hi! I had this problem too and after a lot of effort i figured out that using DOM, there isn't any way you can rename an element directly. May be i am wrong but at least i couldn't find its solution. I changed the structure of my xml file and used a generic name for xml element and stored the data as attribute...e.g. This way you can easily modify the attributes of element. Another possible way (if you don't want to change the structure of xml file) might be by storing the innerXml of node to be renamed, then delete that node and create new node with modified name and assign innerXml to that new node. Hope that helps, if i have made any mistake please correct me. I would love to know if there is any other way of renaming an element. Thanks, Regards, Affan Ahmad Toor .................. QUAIDIAN FOR ONCE, QUAIDIAN FOR EVER!

      P 1 Reply Last reply
      0
      • A Affan Toor

        Hi! I had this problem too and after a lot of effort i figured out that using DOM, there isn't any way you can rename an element directly. May be i am wrong but at least i couldn't find its solution. I changed the structure of my xml file and used a generic name for xml element and stored the data as attribute...e.g. This way you can easily modify the attributes of element. Another possible way (if you don't want to change the structure of xml file) might be by storing the innerXml of node to be renamed, then delete that node and create new node with modified name and assign innerXml to that new node. Hope that helps, if i have made any mistake please correct me. I would love to know if there is any other way of renaming an element. Thanks, Regards, Affan Ahmad Toor .................. QUAIDIAN FOR ONCE, QUAIDIAN FOR EVER!

        P Offline
        P Offline
        Phrone
        wrote on last edited by
        #3

        I cant change the structure of my xml files, because I have over 4000 of them. ;) What I tried is to copy a node if it is named for example niv1, delete it, and create a new node with the new name and add the content that I copied erlier. But I dont get it to work.

        D A 2 Replies Last reply
        0
        • P Phrone

          I cant change the structure of my xml files, because I have over 4000 of them. ;) What I tried is to copy a node if it is named for example niv1, delete it, and create a new node with the new name and add the content that I copied erlier. But I dont get it to work.

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #4

          Phrone wrote:

          What I tried is to copy a node if it is named for example niv1, delete it, and create a new node with the new name and add the content that I copied erlier. But I dont get it to work.

          That's what I'd think is way to do it, what problem do you have with this approach?


          "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

          P 1 Reply Last reply
          0
          • P Phrone

            I cant change the structure of my xml files, because I have over 4000 of them. ;) What I tried is to copy a node if it is named for example niv1, delete it, and create a new node with the new name and add the content that I copied erlier. But I dont get it to work.

            A Offline
            A Offline
            Affan Toor
            wrote on last edited by
            #5

            try doing this: string nodeData = ""; XmlDocument xmlDoc = new XmlDocument(); XmlNode root = xmlDoc.DocumentElement(); XmlNodeList nodeList = root.GetElementByTagName("nav1"); XmlNode node = nodeList[0]; if(node!=null) { nodeData = node.InnerXml; root.RemoveChild(node); //Create new node with modified name XmlElement newNode = xmlDoc.CreateElement("New nav1"); newNode.InnerXml = nodeData; root.AppendChild(newNode); } hope that helps... .................. QUAIDIAN FOR ONCE, QUAIDIAN FOR EVER!

            P 1 Reply Last reply
            0
            • P Phrone

              I have a xml file that looks something like this something something else some text something more bla bla more headlines some simple text... (1) list I've tried to rename some elements using XmlDocument() but with no success. So if anyone can tell me how it is done, I would be glad. Elements to rename: niv1, niv2 and niv3 And ofcourse, save the file with changes.

              E Offline
              E Offline
              Ed Poore
              wrote on last edited by
              #6

              I'm going to be slightly annoying here but that is exactly what XSLT[^] was designed for.  You can execute the transformations from .NET by using the XslTransformation (I think that's it's name) class.

              P 1 Reply Last reply
              0
              • A Affan Toor

                try doing this: string nodeData = ""; XmlDocument xmlDoc = new XmlDocument(); XmlNode root = xmlDoc.DocumentElement(); XmlNodeList nodeList = root.GetElementByTagName("nav1"); XmlNode node = nodeList[0]; if(node!=null) { nodeData = node.InnerXml; root.RemoveChild(node); //Create new node with modified name XmlElement newNode = xmlDoc.CreateElement("New nav1"); newNode.InnerXml = nodeData; root.AppendChild(newNode); } hope that helps... .................. QUAIDIAN FOR ONCE, QUAIDIAN FOR EVER!

                P Offline
                P Offline
                Phrone
                wrote on last edited by
                #7

                This code gives me errors. Error 1 'System.Xml.XmlDocument.DocumentElement' is a 'property' but is used like a 'method' U:\ModXML\ModXML\GUI.cs 265 43 ModXML Error 2 'System.Xml.XmlNode' does not contain a definition for 'GetElementByTagName' U:\ModXML\ModXML\GUI.cs 266 49 ModXML

                Affan Toor wrote:

                try doing this: string nodeData = ""; XmlDocument xmlDoc = new XmlDocument(); XmlNode root = xmlDoc.DocumentElement(); XmlNodeList nodeList = root.GetElementByTagName("nav1"); XmlNode node = nodeList[0]; if(node!=null) { nodeData = node.InnerXml; root.RemoveChild(node); //Create new node with modified name XmlElement newNode = xmlDoc.CreateElement("New nav1"); newNode.InnerXml = nodeData; root.AppendChild(newNode); } hope that helps...

                1 Reply Last reply
                0
                • E Ed Poore

                  I'm going to be slightly annoying here but that is exactly what XSLT[^] was designed for.  You can execute the transformations from .NET by using the XslTransformation (I think that's it's name) class.

                  P Offline
                  P Offline
                  Phrone
                  wrote on last edited by
                  #8

                  I dont know so mutch about XSLT, but isn't it so that it doesn't change the element names only the apperance?

                  Ed.Poore wrote:

                  I'm going to be slightly annoying here but that is exactly what XSLT[^] was designed for. You can execute the transformations from .NET by using the XslTransformation (I think that's it's name) class.

                  E 1 Reply Last reply
                  0
                  • D DavidNohejl

                    Phrone wrote:

                    What I tried is to copy a node if it is named for example niv1, delete it, and create a new node with the new name and add the content that I copied erlier. But I dont get it to work.

                    That's what I'd think is way to do it, what problem do you have with this approach?


                    "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

                    P Offline
                    P Offline
                    Phrone
                    wrote on last edited by
                    #9

                    dnh wrote:

                    Phrone wrote: What I tried is to copy a node if it is named for example niv1, delete it, and create a new node with the new name and add the content that I copied erlier. But I dont get it to work. That's what I'd think is way to do it, what problem do you have with this approach?

                    I'm not sure what I do wrong. And I've tried to find code samples to see how other done, but with no luck. MSDN doesn't have much information about it either.

                    1 Reply Last reply
                    0
                    • P Phrone

                      I dont know so mutch about XSLT, but isn't it so that it doesn't change the element names only the apperance?

                      Ed.Poore wrote:

                      I'm going to be slightly annoying here but that is exactly what XSLT[^] was designed for. You can execute the transformations from .NET by using the XslTransformation (I think that's it's name) class.

                      E Offline
                      E Offline
                      Ed Poore
                      wrote on last edited by
                      #10

                      No, basically it's a scripting language which can transform one xml document into another (text) document.  You can get it to output any form of text, plain text, xml, html etc.  The transformations won't affect the original file unless you overwrite it.  What you may be thinking of is you can link a stylesheet into an xml file (link a css stylesheet into html) and when IE or FF go to display it they try to find the stylesheet and apply the transformation (what it'll normally output is a nice-pretty formatted html version of the xml file so that it's easier to read than a plain xml file).

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups