How to rename elements in a xml file
-
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.
-
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.
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!
-
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!
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.
-
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.
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
-
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.
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!
-
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.
-
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!
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...
-
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.
-
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
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.
-
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.
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).