Groups | Blog | Home
all groups > dotnet xml > december 2005 >

dotnet xml : How to change name of an XML node


KK
12/2/2005 6:20:50 AM
Hi,

Is there any way to change the name of XML node? In the following xml,
I want to change the node <FIELD_1> to <FIELD_ITEM> in all the places.


Ex:
<NewDataSet>
<LOOKUP>
<FIELD_1>First_Item113</FIELD_1>
<FIELD_2>First_Item224</FIELD_2>
</LOOKUP>
<LOOKUP>
<FIELD_1>Second_Item11</FIELD_1>
<FIELD_2>Second_Item222</FIELD_2>
</LOOKUP>
<LOOKUP>
<FIELD_1>Third_Item11</FIELD_1>
<FIELD_2>Third_Item22</FIELD_2>
</LOOKUP>
</NewDataSet>

I am using the following asp.net code, and I am able to read FIELD_1 in
localname but I am not able to change. any ideas?

Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
'Create the XML Document
m_xmld = New XmlDocument
'Load the Xml file
m_xmld.Load("c:\temp\test.xml")
'Get the list of name nodes
m_nodelist = m_xmld.SelectNodes("/NewDataSet/LOOKUP")
Dim i As Integer
For Each m_node In m_nodelist
'CHANGE LOCALNAME
Next
m_xmld.Save("c:\temp\test.xml")


Thanks
Martin Honnen
12/2/2005 3:57:47 PM


[quoted text, click to view]


[quoted text, click to view]

No, the DOM as implemented in .NET does not allow you to change the name
of an already created node, you would need to create a new node with the
desired name, then make sure you copy attributes as needed, then move
child nodes as needed, then replace the old node with the new node.

Or you could write an XSLT stylesheet doing such stuff.

--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button