DOM class
-
Hi! please tell me hOw we can change Attribute Names of a particular "element" after loading document in XmlDocument class. Thanks
Here is a way that keeps the attribute's orignal value and position: There may be a simplier solution.
using System; using System.Xml; namespace CSharp { class Program { static void Main(string[] args) { String xml = "<?xml version='1.0' encoding='UTF-8'?>" + "<book name='Introduction to C# 2.0'/>"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); XmlElement docElem = xmlDoc.DocumentElement; XmlAttribute attribName = xmlDoc.DocumentElement.Attributes["name"]; XmlAttribute attribTitle = xmlDoc.CreateAttribute("title"); attribTitle.Value = attribName.Value; docElem.Attributes.InsertBefore(attribTitle, attribName); docElem.Attributes.Remove(attribName); Console.WriteLine(xmlDoc.OuterXml); } } }