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/