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
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/
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" <FamilyT...@ThisOldHouse.com> wrote in message
news:%23KqOHvb...@TK2MSFTNGP03.phx.gbl...
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
> 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
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
> 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");