473,808 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2062
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(str ing);

then you do this:

XmlNode docroot = doc.DocumentEle ment;

// 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.CreateEleme nt("the new name here");

// now we get all the root node children and add them to this.

foreach (XmlNode child in docroot.ChildNo des) {

newnode.AppendC hild(child);
}

//then all we do is remove the reference to the old docroot with the
new one.

doc.DocumentEle ment = 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
1483
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() myXml.Load(Server.MapPath("XSLLinks.xslt")) Dim XmlLinks As XmlElement Dim XmlNewLink As XmlElement
9
3719
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 the fastest or most efficient way to do this?
3
16301
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 of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
8
11362
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" standalone="yes"?> <Files> <File> <Text>Test</Text> <Name>Test.html</Name> </File>
2
1643
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 open an XML file who is located on the root website: XmlDocument _XmlParametres = new XmlDocument( );
0
1104
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 from sql server and subsequently an xmldocument can't be created from the xmltextwriter as there is no root element. however if the xml raw query returns only a single row then there is a root
1
4420
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
15084
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 simply persist any changes i made, but it doesn't happen. e.g. selected.ParentNode.ChildNodes.Remove(selected); throws a NotSupportedException "Collection is read-only"
2
14205
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" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="TravelDraft"> <DraftId>139451de-1bc1-4070-b9d1-548dd4a7f812</DraftId> <StartDate>2006-10-04T00:00:00</StartDate> <RetryCount>0</RetryCount> <LocalCurrencyId>1</LocalCurrencyId> <EndDate>2006-10-25T23:59:59</EndDate>
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10631
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.