How to read mutiple rootelements 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,

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

only this i want to copy and put in another .xml file:

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

I can do it this way


XmlDocument xdoc = new XmlDocument();
xdoc.Load(filename);

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.

XmlDocument docXML = new XmlDocument();
docXML.Load(new file);
docXML .DocumentElement.SetAttribute("ConfigMax", strConfigMax);

But my rootelement keeps changing and all the times i may not
know the attribute name


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


please help me on this


thanks in advance
RAGHU
 
N

Nicholas Paldino [.NET/C# MVP]

RAGHU,

Why not just create a copy of the document, remove all the child
elements, and then add that to the new file?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

<?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,

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

only this i want to copy and put in another .xml file:

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

I can do it this way


XmlDocument xdoc = new XmlDocument();
xdoc.Load(filename);

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.

XmlDocument docXML = new XmlDocument();
docXML.Load(new file);
docXML .DocumentElement.SetAttribute("ConfigMax", strConfigMax);

But my rootelement keeps changing and all the times i may not
know the attribute name


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


please help me on this


thanks in advance
RAGHU
 

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