473,473 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to copy the whole RootElement in an .xml file

<?xml version="1.0" standalone="yes" ?>
- <AddressSpace xsi:noNamespaceSchemaLocation="prince.xsd" Name="U2"
ConfigMax="1" xmlns:xsi="http://www.rag.org/2001/XMLSchema-instance">
<Item Name="raj" VALUE="60864"/>
<Item Name="rag" VALUE="60868" />
</AddressSpace>
Hi all,

I have a .xml file like this.I have to do lot of operations by reading
comparing and copying some data to other files like that.

Now my problem i am struck:- I want to copy the full Root element and
put it in another .xml file

<AddressSpace xsi:noNamespaceSchemaLocation="prince.xsd" Name="U2"
ConfigMax="1" xmlns:xsi="http://www.rag.org/2001/XMLSchema-instance">

root element i want to copy

I can do it this way

string strfiletype =
xdoc.DocumentElement.GetAttribute("xsi:noNamespace SchemaLocation".ToString());
string strName =
xdoc.DocumentElement.GetAttribute("Name".ToString( ));
string strConfigMax =
xdoc.DocumentElement.GetAttribute("ConfigMax".ToSt ring());

....

and then use setattribute to write to a new .xml file.

But my rootelement keeps chnaging and all the times it is may not
know the attribute name

hence i want to copy whole rootelement without using any names and put
it in a new .xml file.

please help me on this

thanks in advance
RAGHU

Nov 3 '08 #1
7 3822
ra*****@gmail.com wrote:
<?xml version="1.0" standalone="yes" ?>
- <AddressSpace xsi:noNamespaceSchemaLocation="prince.xsd" Name="U2"
ConfigMax="1" xmlns:xsi="http://www.rag.org/2001/XMLSchema-instance">
<Item Name="raj" VALUE="60864"/>
<Item Name="rag" VALUE="60868" />
</AddressSpace>
Hi all,

I have a .xml file like this.I have to do lot of operations by reading
comparing and copying some data to other files like that.

Now my problem i am struck:- I want to copy the full Root element and
put it in another .xml file

<AddressSpace xsi:noNamespaceSchemaLocation="prince.xsd" Name="U2"
ConfigMax="1" xmlns:xsi="http://www.rag.org/2001/XMLSchema-instance">
You can use ImportNode on the DocumentElement e.g.

XmlDocument doc1 = new XmlDocument();
doc1.Load(@"XMLFile1.xml");

XmlDocument doc2 = new XmlDocument();
doc2.AppendChild(doc2.ImportNode(doc1.DocumentElem ent, false));
doc2.Save("XMLFile2.xml");
That way you copy the element and its attributes from doc1 to doc2.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 3 '08 #2
I found this trick once to do this (copy docInput DocumentElement to
docOutput):
XmlNode n = docOutput.ImportNode(docInput.DocumentElement, true);
docOutput.DocumentElement.AppendChild(n);

Hope this helps.

<ra*****@gmail.comwrote in message
news:0e**********************************@n33g2000 pri.googlegroups.com...
<?xml version="1.0" standalone="yes" ?>
- <AddressSpace xsi:noNamespaceSchemaLocation="prince.xsd" Name="U2"
ConfigMax="1" xmlns:xsi="http://www.rag.org/2001/XMLSchema-instance">
<Item Name="raj" VALUE="60864"/>
<Item Name="rag" VALUE="60868" />
</AddressSpace>
Hi all,

I have a .xml file like this.I have to do lot of operations by reading
comparing and copying some data to other files like that.

Now my problem i am struck:- I want to copy the full Root element and
put it in another .xml file

<AddressSpace xsi:noNamespaceSchemaLocation="prince.xsd" Name="U2"
ConfigMax="1" xmlns:xsi="http://www.rag.org/2001/XMLSchema-instance">

root element i want to copy

I can do it this way

string strfiletype =
xdoc.DocumentElement.GetAttribute("xsi:noNamespace SchemaLocation".ToString());
string strName =
xdoc.DocumentElement.GetAttribute("Name".ToString( ));
string strConfigMax =
xdoc.DocumentElement.GetAttribute("ConfigMax".ToSt ring());

...

and then use setattribute to write to a new .xml file.

But my rootelement keeps chnaging and all the times it is may not
know the attribute name

hence i want to copy whole rootelement without using any names and put
it in a new .xml file.

please help me on this

thanks in advance
RAGHU
Nov 3 '08 #3
I just saw Martin's response. The difference in the true/false argument to
ImportNode is whether the subelements come along for the ride. It was not
clear if you wanted them or not.

"Family Tree Mike" <Fa************@ThisOldHouse.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>I found this trick once to do this (copy docInput DocumentElement to
docOutput):
XmlNode n = docOutput.ImportNode(docInput.DocumentElement, true);
docOutput.DocumentElement.AppendChild(n);

Hope this helps.

<ra*****@gmail.comwrote in message
news:0e**********************************@n33g2000 pri.googlegroups.com...
><?xml version="1.0" standalone="yes" ?>
- <AddressSpace xsi:noNamespaceSchemaLocation="prince.xsd" Name="U2"
ConfigMax="1" xmlns:xsi="http://www.rag.org/2001/XMLSchema-instance">
<Item Name="raj" VALUE="60864"/>
<Item Name="rag" VALUE="60868" />
</AddressSpace>
Hi all,

I have a .xml file like this.I have to do lot of operations by reading
comparing and copying some data to other files like that.

Now my problem i am struck:- I want to copy the full Root element and
put it in another .xml file

<AddressSpace xsi:noNamespaceSchemaLocation="prince.xsd" Name="U2"
ConfigMax="1" xmlns:xsi="http://www.rag.org/2001/XMLSchema-instance">

root element i want to copy

I can do it this way

string strfiletype =
xdoc.DocumentElement.GetAttribute("xsi:noNamespac eSchemaLocation".ToString());
string strName =
xdoc.DocumentElement.GetAttribute("Name".ToString ());
string strConfigMax =
xdoc.DocumentElement.GetAttribute("ConfigMax".ToS tring());

...

and then use setattribute to write to a new .xml file.

But my rootelement keeps chnaging and all the times it is may not
know the attribute name

hence i want to copy whole rootelement without using any names and put
it in a new .xml file.

please help me on this

thanks in advance
RAGHU
Nov 3 '08 #4
On Nov 3, 7:03*pm, "Family Tree Mike"
<FamilyTreeM...@ThisOldHouse.comwrote:
I just saw Martin's response. *The difference in the true/false argument to
ImportNode is whether the subelements come along for the ride. *It was not
clear if you wanted them or not.

"Family Tree Mike" <FamilyTreeM...@ThisOldHouse.comwrote in messagenews:%2****************@TK2MSFTNGP03.phx.gb l...
I found this trick once to do this (copy docInput DocumentElement to
docOutput):
* XmlNode n = docOutput.ImportNode(docInput.DocumentElement, true);
* docOutput.DocumentElement.AppendChild(n);
Hope this helps.
<ragh...@gmail.comwrote in message
news:0e**********************************@n33g2000 pri.googlegroups.com....
<?xml version="1.0" standalone="yes" ?>
- <AddressSpace xsi:noNamespaceSchemaLocation="prince.xsd" Name="U2"
ConfigMax="1" xmlns:xsi="http://www.rag.org/2001/XMLSchema-instance">
*<Item Name="raj" VALUE="60864"/>
*<Item Name="rag" VALUE="60868" />
</AddressSpace>
Hi all,
I have a .xml file like this.I have to do lot of operations by reading
comparing and copying some data to other files like that.
Now my problem i am struck:- I want to copy the full Root element and
put it in another .xml file
<AddressSpace xsi:noNamespaceSchemaLocation="prince.xsd" Name="U2"
ConfigMax="1" xmlns:xsi="http://www.rag.org/2001/XMLSchema-instance">
root element i want to copy
I can do it this way
* * * * * * string strfiletype =
xdoc.DocumentElement.GetAttribute("xsi:noNamespace SchemaLocation".ToString(*));
* * * * * *string strName =
xdoc.DocumentElement.GetAttribute("Name".ToString( ));
* * * * * string strConfigMax =
xdoc.DocumentElement.GetAttribute("ConfigMax".ToSt ring());
...
and then use setattribute to write to a new .xml file.
But my rootelement keeps chnaging and all the times it is *may not
know the attribute name
hence i want to copy whole rootelement without using any names and put
it in a new .xml file.
please help me on this
thanks in advance
RAGHU- Hide quoted text -

- Show quoted text -
Hi all,

Thnx all for your reply,i just want to copy only the rooteement and
write in a new .xml file only the rootelement.

The other things should not be copied.

<AddressSpace xsi:noNamespaceSchemaLocation="prince.xsd" Name="U2"
ConfigMax="1" xmlns:xsi="http://www.rag.org/2001/XMLSchema-instance">
only this i will copy and put it in a new .xml file

can anyone please tell me on this

Thanks in advance,
RAGHU
Nov 3 '08 #5
ra*****@gmail.com wrote:
Thnx all for your reply,i just want to copy only the rooteement and
write in a new .xml file only the rootelement.
I did show you how to do that in
http://groups.google.com/group/micro...18fc77247f5837
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 3 '08 #6
On Nov 3, 10:58*pm, Martin Honnen <mahotr...@yahoo.dewrote:
ragh...@gmail.com wrote:
Thnx all for your reply,i just want to copy only the rooteement and
write in a new .xml file only the rootelement.

I did show you how to do that inhttp://groups.google.com/group/microsoft.public.dotnet.languages.csha...

--

* * * * Martin Honnen --- MVP XML
* * * *http://JavaScript.FAQTs.com/
Hi martin,

your solution is working, but one problem is there:-

XmlDocument doc1 = new XmlDocument();
doc1.Load(@"XMLFile1.xml");
XmlDocument doc2 = new XmlDocument();
doc2.Load(@"newfile.xml"); -->(This is the change in my
code- i have loaded a new file- the new file is a result of lot of
operations done using the dataset class
with
rootelement as only <Addressspace/>.I want to open the new file
replace only the rootelement line to proper one(full
l line) and
then save it.I don't want to touch any nodes.

doc2.AppendChild(doc2.ImportNode(doc1.DocumentElem ent,
false)); //if i put this code the newfile .xml file will have only the
rootelement and nothing else
doc2.Save("XMLFile2.xml");

please help on this

thanks in advance,
RAGHU
Nov 3 '08 #7
ra*****@gmail.com wrote:
XmlDocument doc1 = new XmlDocument();
doc1.Load(@"XMLFile1.xml");
XmlDocument doc2 = new XmlDocument();
doc2.Load(@"newfile.xml"); -->(This is the change in my
code- i have loaded a new file- the new file is a result of lot of
operations done using the dataset class
with
rootelement as only <Addressspace/>.I want to open the new file
replace only the rootelement line to proper one(full
l line) and
then save it.I don't want to touch any nodes.

doc2.AppendChild(doc2.ImportNode(doc1.DocumentElem ent,
false)); //if i put this code the newfile .xml file will have only the
rootelement and nothing else
doc2.Save("XMLFile2.xml");
You can't solve that with a single operation. You need to store the
DocumentElement first e.g.
XmlNode oldRoot = doc2.DocumentElement;
XmlNode newRoot = doc2.ImportNode(doc1.DocumentElement, false);
doc2.ReplaceChild(newRoot, oldRoot);
while (oldRoot.HasChildNodes)
{
newRoot.AppendChild(oldRoot.FirstChild);
}
doc2.Save("XMLFile2.xml");
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 3 '08 #8

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

Similar topics

5
by: DraguVaso | last post by:
Hi, I'm looking for a way to Copy and Paste Files to the clipboard. I found a lot of articles to copy pieces of text and bitmaps etc, but nog whole files. Whay I need is like you have in...
5
by: DraguVaso | last post by:
Hi, I need a SECURE way to copy parts of a file. I'm having files which contains a whole bunch of records. In one 'fysical' file I'm having one or more logical files. What I need to do is to...
2
by: Xavier Valdés | last post by:
Hi all, I would like to copy FILES from the clipboard to a desired folder from VB.NET. I was able to copy files (with filedrop data format) to the clipboard but I don't know how to catch this...
2
by: C G | last post by:
Dear All, I'm trying to insert an xml file into my database. I have a table with a single text column. My intention is just to have the xml file take up one row in the table. I've tried the...
1
by: Rachel McConnell | last post by:
Hi, I am trying to import data using COPY, from a file containing thirty or so COPY commands each with 0 or more rows of data. Reason, I have a small data set I want to include into a database...
6
by: sql_server_user | last post by:
I'm trying to copy all 440 million rows from one table in my SQL Server 2005 db to another table with a different clustering scheme. After a few test inserts that were successful (up to a million...
4
by: eholz1 | last post by:
Hello PHP group, I am trying to use the copy function to copy files from one directory to another. This does not seem to work for me. What am I missing? Below is the code I am using. ...
4
by: pradqdo | last post by:
Hi folks, I have a very strange problem when I try to port my client/server program to cygwin. It is a simple shell program where the server executes client's commands + it can send and receive...
1
by: veer | last post by:
hi it looks a silly question but i m getting confused actually i want to copy folders from one location to another like from C:\abc To D:\xyz here abc and xyz are two folders in c and d drive it...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
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...

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.