Fengzhimei@Dot.Net
Designing My Colorful Dream
    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.
posted on 2004-04-21 11:16  fengzhimei  阅读(921)  评论(2编辑  收藏  举报