I have a bulk XML file and I want to change the attributes of a certain node to an element. For instance
< book id=12 >
< author > Tamer < / author >
< / book >
I want it to be
< book >
< id > 12 < / id >
< author > Tamer < / author >
< / book >
How to convert XML attribute to Element using C#?
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.


Roei BarRank #1,271 · 1.4K reputationPosted Sep 10, 2009, 12:47 PM EDT
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(); XmlNode selectedNode = xmlDoc.SelectSingleNode("./"); selectdeNode = changeNodeAttributesToElements(selectdeNode,xmlDoc); private XmlNode changeNodeAttributesToElements(XmlNode node,XmlDocument xmlDoc) { foreach (XmlAttribute att in node.Attributes) { XmlNode newNode = xmlDoc.CreateElement(att.Name); newNode.InnerText = att.Value; node.Append(newNode); } node.Attributes.RemoveAll(); }Tamer KhalilRank #2,182 · 647 reputationPosted Sep 10, 2009, 1:34 PM EDT
Roei BarRank #1,271 · 1.4K reputationPosted Sep 11, 2009, 3:21 AM EDT
send me the code you used