> XmlDocument xml = new XmlDocument();
>
[quoted text clipped - 8 lines]
> 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>

Signature
Martin Honnen
http://JavaScript.FAQTs.com/
Jame Healy - 30 Oct 2003 20:07 GMT
Hi Martin,
I've been working on something similar, but trying to generalize it as well.
The concept is a function that will take two parameters (XmlDocument
inboundDocument, string attributeName) and will go through the XML document
recursively renaming elements by the value of their name attribute (if it
exists).
Sample input:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<element name="Supergod">
<element name="GodName">Kibo</element>
<element name="GodHome">http://www.kibo.com/</element>
</element>
</root>
Required output:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Supergod>
<GodName>Kibo</GodName>
<GodHome>http://www.kibo.com/</GodHome>
</Supergod>
</root>
I'm curious how you would approach this problem.
> > XmlDocument xml = new XmlDocument();
> >
[quoted text clipped - 58 lines]
> value="Supergod"><name>Kibo
> </name><home>http://www.kibo.com/</home></super-god></gods>