How to copy One XMLdocument to another?
-
Hello Friends I am reading a xml file(suppose A.xml) using MSXML::IXMLDOMDocumentPtr and all related classes of MSXML. Now,I want to create another xml file with new data and want to replace with A.xml. Both xml will be having same Nodes but their child nodes can be different. Now,My question is Is there any way that I can store all the data in DomDocumentPtr object and then replace with it existing xml file Dom Pointer? Or do i need to create another xml on disc and then i have to replace one file with another file. Which way i can achieve this? Thanks and Regards Yogesh sikri
-
Hello Friends I am reading a xml file(suppose A.xml) using MSXML::IXMLDOMDocumentPtr and all related classes of MSXML. Now,I want to create another xml file with new data and want to replace with A.xml. Both xml will be having same Nodes but their child nodes can be different. Now,My question is Is there any way that I can store all the data in DomDocumentPtr object and then replace with it existing xml file Dom Pointer? Or do i need to create another xml on disc and then i have to replace one file with another file. Which way i can achieve this? Thanks and Regards Yogesh sikri
XmlDocument::ImportNode does the job, It states and i quote: "The
ImportNode
method is the mechanism by which a node or entire node subtree is copied from one XmlDocument to another. The node returned from the call is a copy of the node from the source document, including attribute values, the node name, node type, and all namespace-related attributes such as the prefix, local name, and namespace Uniform Resource Identifier (URI). The source document is not changed. " more from MSDN.. Code from MSDN:#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{//Create the XmlDocument.
XmlDocument^ doc = gcnew XmlDocument;
doc->LoadXml( "<bookstore><book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book></bookstore>" );//Create another XmlDocument which holds a list of books.
XmlDocument^ doc2 = gcnew XmlDocument;
doc2->Load( "books.xml" );//Import the last book node from doc2 into the original document.
XmlNode^ newBook = doc->ImportNode( doc2->DocumentElement->LastChild, true );
doc->DocumentElement->AppendChild( newBook );
Console::WriteLine( "Display the modified XML..." );
doc->Save( Console::Out );
}