473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

how can I change node name

how can I change the name of XML node?
this is sample

<?xml version="1.0"?>
<adresboek>
<adres>
<naam>Mark van Renswoude</naam>
<straat>ddden 24</straat>
<plaats>Scddd</plaats>
<email>ma@xe.net</email>
</adres>
</adresboek>
I copy this sample with using Clone();
and how can I change node name overthere.

<?xml version="1.0"?>
<adresboek>
<adres>
<naam>Mark van Renswoude</naam>
<straat>xxxxx 25</straat>
<plaats>Scddd</plaats>
<email>ma@xe.net</email>
</adres>
<adres_old>------------------------------change name
<naam>Mark van Renswoude</naam>
<straat>ddden 24</straat>
<plaats>Scddd</plaats>
<email>ma@xe.net</email>
</adres_old> ----------------------------change name

</adresboek>
Nov 11 '05 #1
3 23064


zerdust wrote:
how can I change the name of XML node?
this is sample

<?xml version="1.0"?>
<adresboek>
<adres>
<naam>Mark van Renswoude</naam>
<straat>ddden 24</straat>
<plaats>Scddd</plaats>
<email>ma@xe.net</email>
</adres>
</adresboek>
I copy this sample with using Clone();
and how can I change node name overthere.

<?xml version="1.0"?>
<adresboek>
<adres>
<naam>Mark van Renswoude</naam>
<straat>xxxxx 25</straat>
<plaats>Scddd</plaats>
<email>ma@xe.net</email>
</adres>
<adres_old>------------------------------change name
<naam>Mark van Renswoude</naam>
<straat>ddden 24</straat>
<plaats>Scddd</plaats>
<email>ma@xe.net</email>
</adres_old> ----------------------------change name

</adresboek>


You can't change the name of a node, rather you need to create a new
node with a different name.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 11 '05 #2
XmlDocument xml = new XmlDocument();

xml.Load("c:\x.xml");

XmlNode rootTextNode = xml.SelectSingleNode("vvt/dd");

XmlNode root = xml.SelectSingleNode("vvt");

XmlNode cloneTextNode = rootTextNode.Clone();

cloneTextNode. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< is it
possible to change name here?

root.AppendChild(cloneTextNode);

xml.Save("c:\\b.xml");

"Martin Honnen" <Ma***********@t-online.de> wrote in message
news:OK**************@TK2MSFTNGP11.phx.gbl...


zerdust wrote:
how can I change the name of XML node?
this is sample

<?xml version="1.0"?>
<adresboek>
<adres>
<naam>Mark van Renswoude</naam>
<straat>ddden 24</straat>
<plaats>Scddd</plaats>
<email>ma@xe.net</email>
</adres>
</adresboek>
I copy this sample with using Clone();
and how can I change node name overthere.

<?xml version="1.0"?>
<adresboek>
<adres>
<naam>Mark van Renswoude</naam>
<straat>xxxxx 25</straat>
<plaats>Scddd</plaats>
<email>ma@xe.net</email>
</adres>
<adres_old>------------------------------change name
<naam>Mark van Renswoude</naam>
<straat>ddden 24</straat>
<plaats>Scddd</plaats>
<email>ma@xe.net</email>
</adres_old> ----------------------------change name

</adresboek>


You can't change the name of a node, rather you need to create a new
node with a different name.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 11 '05 #3


zerdust wrote:
XmlDocument xml = new XmlDocument();

xml.Load("c:\x.xml");

XmlNode rootTextNode = xml.SelectSingleNode("vvt/dd");

XmlNode root = xml.SelectSingleNode("vvt");

XmlNode cloneTextNode = rootTextNode.Clone();

cloneTextNode. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< is it
possible to change name here?


No, as said, you cannot simply chane the name, you need to create a new
element node with the desired name, here is an example:

using System.Xml;

public class Test20031016 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20031011.xml");
System.Console.WriteLine(xmlDocument.OuterXml);
XmlElement element = (XmlElement)
xmlDocument.GetElementsByTagName("god")[0];
XmlElement newElement = copyElementToName(element, "super-god");
xmlDocument.DocumentElement.AppendChild(newElement );
System.Console.WriteLine(xmlDocument.OuterXml);
}

public static XmlElement copyElementToName (XmlElement element,
string tagName) {
XmlElement newElement = element.OwnerDocument.CreateElement(tagName);
for (int i = 0; i < element.Attributes.Count; i++) {
newElement.SetAttributeNode((XmlAttribute)
element.Attributes[i].CloneNode(true));
}
for (int i = 0; i < element.ChildNodes.Count; i++) {
newElement.AppendChild(element.ChildNodes[i].CloneNode(true));
}
return newElement;
}
}

That function copyElementToName allows you to do what you want, if the
document is originally

<?xml version="1.0" encoding="UTF-8"?>
<gods>
<god value="Supergod">
<name>Kibo</name>
<home>http://www.kibo.com/</home>
</god>
</gods>

then the program outputs

<?xml version="1.0" encoding="UTF-8"?><gods><god
value="Supergod"><name>Kibo</na
me><home>http://www.kibo.com/</home></god><super-god
value="Supergod"><name>Kibo
</name><home>http://www.kibo.com/</home></super-god></gods>
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex Sab | last post by:
Hi, I am having trouble describing in a schema that elements can be mandatory in one part and not mandatory in another part of the xml document. Here is a sample xml file <Model:Templates>...
15
by: Henning Vestergaard Poulsen | last post by:
Hi, I have a problem that I hope someone can help me with. I'm building a web page with pictures I've taken with my digital camera. I have succeded making a javacript that, when clicking on a...
3
by: Blaise Garant | last post by:
Hi I've made a stylesheet to transform my data into XSL-FO. This stylesheet used to work with MSXSL 4.0 but I've got some issues in ..NET. First, I changed removed all the "node-set()" function...
2
by: Rick | last post by:
I have an XML document that is generated from Infopath, I need to change the value of a namespace that is defined in a node in the form: <xsf:xDocumentClass "xmlns:my=valuehere">. when i navigate...
1
by: KK | last post by:
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>...
0
by: KK | last post by:
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>...
3
by: ina | last post by:
Hello all, I have this xml file: <FS> <Style> <Name>Luca</Name> <Strategy> <Strategy_Style>Geo\Asia</Strategy_Style> <Strategy_Style>Geo\America</Strategy_Style>
7
by: dotnetnoob | last post by:
i keep getting Object references not set to an instance of an object from this code: Private Sub EqBinding() Dim x As Integer x = 0 Do If CStr(arlsType.Item(x)) = "Bacnet Point" Then Dim...
0
by: =?Utf-8?B?amFjb2I=?= | last post by:
hello guys i have a problems with xmls how to set or change values from xml node elements from an existent xml file using C# from this xml file <xml version="1.0"> <students>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.