How to merge two XmlDocuments in C#

后端 未结 4 881
半阙折子戏
半阙折子戏 2020-12-14 17:33

I want to merge two XmlDocuments by inserting a second XML doc to the end of an existing Xmldocument in C#. How is this done?

4条回答
  •  清歌不尽
    2020-12-14 17:46

    Bad news. As long as the xml documents can have only one root element you cannot just put content of one document at the end of the second. Maybe this is what you are looking for? It shows how easily you can merge xml files using Linq-to-XML

    Alternatively if you are using XmlDocuments you can try make it like this:

    XmlDocument documentA;
    XmlDocument documentB;
    
    foreach(var childNode in documentA.DocumentElement.ChildNodes)
       documentB.DocumentElement.AppendChild(childNode);
    

提交回复
热议问题