473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Editing root name using XmlDocument

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

Dec 22 '05 #1
1 2038
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

Dec 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jernej goricki | last post by:
Hy I'm trying to edit a XSLT document, just like I'm editing a XML document : (Im trying to make a new xsl:template tag here ) Dim myXml As New XmlDocument()...
9
by: Jiho Han | last post by:
Suppose I have an xml fragment like: <mother> <child name="Bob" sex="M"/> <child name="Jane" sex="F"/> ... </mother> If I wanted to replace the <mother> element to <father> element, what is...
3
by: Samem N via DotNetMonster.com | last post by:
Does anyone know how to solve this error? I dont know where it went wrong. Any help would be appreciated .Thanks ERROR ______ Description: An unhandled exception occurred during the execution...
8
by: Sam Collett | last post by:
Is there a basic guide on Xml document creation and editing (simpler than the MSDN docs). Say I want to create a file containing the following: <?xml version="1.0" encoding="utf-8"...
2
by: yburtin | last post by:
Hi, I'm currently working on a dotnet web application and here is the problem I have: On my computer (french version of xp pro), I have my webserver installed. Inside one of my webpages, I...
0
by: Martin | last post by:
Hi, I am retriving data from sql server using the xml raw clause and an xmltextwriter. this is working out fine except the xml raw clause does not put a root node on the xml that is returned...
1
by: Diogo Alves - Software Developer | last post by:
Hi, I'm pretty new with XML, and I need to edit a XmlDocument in C#.... now here is an example on what I need. I have this XMLDocument: <?xml version="1.0"?> <root>
5
by: Tim Mackey | last post by:
hi, i have put my web.sitemap in /App_Data so i can edit it programatically via a web admin page, inheriting the modify permissions from the App_Data folder etc. i was hoping the provider would...
2
by: Mikus Sleiners | last post by:
I want to create xml document that looks like this: <?xml version="1.0" encoding="utf-16"?> <Draft xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.