How to copy the whole RootElement in an .xml file

R

raghudr

<?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:noNamespaceSchemaLocation".ToString());
string strName =
xdoc.DocumentElement.GetAttribute("Name".ToString());
string strConfigMax =
xdoc.DocumentElement.GetAttribute("ConfigMax".ToString());

....

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
 
M

Martin Honnen

<?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.DocumentElement, false));
doc2.Save("XMLFile2.xml");


That way you copy the element and its attributes from doc1 to doc2.
 
F

Family Tree Mike

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.
 
F

Family Tree Mike

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.
 
R

raghudr

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.








- 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"
only this i will copy and put it in a new .xml file

can anyone please tell me on this

Thanks in advance,
RAGHU
 
R

raghudr

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

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.DocumentElement,
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
 
M

Martin Honnen

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.DocumentElement,
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");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top