In this implementationtime,I will tell you how to clone a node from a XmlDocument and then insert into another XmlDocument.
And i will do this by two different way,why?yes,I just want to explain which way is the best.
First i need to load data into XmlDocument object,which looks like:
XmlDocument sourceDoc = new XmlDocument();
XmlDocument destDoc = new XmlDocument();
sourceDoc.LoadXml(someSourceFile);
destDoc.LoadXml(someDestFile);
XmlNode sourceNode = sourceDoc.SelectSingleNode(someSourceXPath);
XmlNode destParentNode = destDoc.SelectSingleNode(someDestXPath);
Second is the main method,here i list two method,which looks like:
//Method 1
XmlNode newDestNode = destDoc.CreateNode();
newDestNode.OuterXml = sourceNode.OuterXml;
destParentNode.AppendChild(newDestNode);
//Method 2
XmlNode newDestNode = destDoc.ImportNode(sourceNode,true);
destParentNode.AppendChild(newDestNode);
Because the Method 1 has a process of string serialization and string deserialization,so when the sourceNode XML is very large,the performance is very bad,but Method 2 have not this problem.
By testing,the performance boost is tremendous for large XML structures when switching from method 1 to method 2.
And i will do this by two different way,why?yes,I just want to explain which way is the best.
First i need to load data into XmlDocument object,which looks like:
XmlDocument sourceDoc = new XmlDocument();
XmlDocument destDoc = new XmlDocument();
sourceDoc.LoadXml(someSourceFile);
destDoc.LoadXml(someDestFile);
XmlNode sourceNode = sourceDoc.SelectSingleNode(someSourceXPath);
XmlNode destParentNode = destDoc.SelectSingleNode(someDestXPath);
Second is the main method,here i list two method,which looks like:
//Method 1
XmlNode newDestNode = destDoc.CreateNode();
newDestNode.OuterXml = sourceNode.OuterXml;
destParentNode.AppendChild(newDestNode);
//Method 2
XmlNode newDestNode = destDoc.ImportNode(sourceNode,true);
destParentNode.AppendChild(newDestNode);
Because the Method 1 has a process of string serialization and string deserialization,so when the sourceNode XML is very large,the performance is very bad,but Method 2 have not this problem.
By testing,the performance boost is tremendous for large XML structures when switching from method 1 to method 2.
浙公网安备 33010602011771号