Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to copy the whole RootElement in an .xml file

15 views
Skip to first unread message

rag...@gmail.com

unread,
Nov 3, 2008, 3:51:36 AM11/3/08
to
<?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

Martin Honnen

unread,
Nov 3, 2008, 8:51:36 AM11/3/08
to
rag...@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.DocumentElement, 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/

Family Tree Mike

unread,
Nov 3, 2008, 8:55:08 AM11/3/08
to
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.

<rag...@gmail.com> wrote in message
news:0e4b0ef3-2910-464d...@n33g2000pri.googlegroups.com...

Family Tree Mike

unread,
Nov 3, 2008, 9:03:10 AM11/3/08
to
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" <FamilyT...@ThisOldHouse.com> wrote in message
news:%23KqOHvb...@TK2MSFTNGP03.phx.gbl...

rag...@gmail.com

unread,
Nov 3, 2008, 11:54:11 AM11/3/08
to
On Nov 3, 7:03 pm, "Family Tree Mike"

<FamilyTreeM...@ThisOldHouse.com> wrote:
> 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.com> wrote in messagenews:%23KqOHvb...@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.
>
> > <ragh...@gmail.com> wrote in message

> >news:0e4b0ef3-2910-464d...@n33g2000pri.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: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- 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

Martin Honnen

unread,
Nov 3, 2008, 12:58:37 PM11/3/08
to
rag...@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/microsoft.public.dotnet.languages.csharp/msg/4918fc77247f5837

rag...@gmail.com

unread,
Nov 3, 2008, 1:33:39 PM11/3/08
to
On Nov 3, 10:58 pm, Martin Honnen <mahotr...@yahoo.de> wrote:

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

Martin Honnen

unread,
Nov 3, 2008, 2:00:59 PM11/3/08
to
rag...@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.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");

0 new messages