hi, i am new to xml can any one plz tell me how to change the root name in a xml. <NewDataSet> - <PersonInfo> <ID>3123</ID> <Person>Mohan</Person> </PersonInfo> - <PersonInfo> <ID>3124</ID> <Person>Ram</Person> </PersonInfo> </NewDataSet> how to change <NewDataSet> to some other name using XmlDocument ..... plz help me
hi Sridev, I'm assuming you are using C# here as you mentioned XmlDocument object by VB.NET uses same basic functionality. You can't by default change the name of an already existing node, this is a limitation of the XMLDOM that has been implemented, once created the node name is read only. But all you have to do is create a new one in the same context, and add all of the other's children to it. Here's your pseudo code. XmlDocument doc = new XmlDocument(); doc.Load(file); // if it is the string then use doc.LoadXml(string); then you do this: XmlNode docroot = doc.DocumentElement; // documentElement you see is a special property but is actually returns an XmlNode. Once you have an XmlNode you can do anything you want to it the same as any other node. Don't forget it passes byRef as well so any edits you make will occur on the live document. // so now we create the new node: XmlNode newnode = doc.CreateElement("the new name here"); // now we get all the root node children and add them to this. foreach (XmlNode child in docroot.ChildNodes) { newnode.AppendChild(child); } //then all we do is remove the reference to the old docroot with the new one. doc.DocumentElement = newnode; // done. Cheers AndrewF
Don't see what you're looking for? Try a search.
|