简体   繁体   English

C#将多个XMLNode或XMLNodeList从一个XMLDocument复制到另一个

[英]C# Copying Multiple XMLNodes or XMLNodeList from One XMLDocument to Another

I've searched and searched, but haven't been able to find exactly what I'm looking for. 我已经搜索了,但是还不能完全找到我想要的东西。 It may just be my lack of understanding of XML as this is my first time really playing around with it. 可能只是我缺乏对XML的了解,因为这是我第一次真正使用XML。

Basically I'm connecting to the Pardot API and requesting ALL prospects. 基本上,我正在连接Pardot API并要求所有潜在客户。 However, the data is paged, so I only get 200 prospects at a time, and they are returned in XML format. 但是,数据是分页的,所以我一次只能获得200个潜在客户,并且它们以XML格式返回。 What I need to do is, while in a loop that is continuously going through the paged data and getting XML responses, I need to copy each response's XML data over to a new document that, at the end, will have the contents of ALL the responses. 我需要做的是,在一个循环遍历分页数据并获取XML响应的循环中,我需要将每个响应的XML数据复制到一个新文档中,最后,该文档将包含所有回应。

I have something like this, which works upon the first iteration, but during the the next one it fails at the finalXmlDoc.AppendChile(impNode); 我有类似的东西,它可以在第一次迭代中工作,但是在下一个迭代中,它会在finalXmlDoc.AppendChile(impNode);处失败finalXmlDoc.AppendChile(impNode); with error message: "This document already has a 'DocumentElement' node." 错误消息:“此文档已经有一个'DocumentElement'节点。”

            var xmlDoc = new XmlDocument();
            var finalXmlDoc = new XMLDocument();

            while (true)
            {
                 //BUILD URL FOR EACH ITERATION AND DO AN HTTP GET                                       

                 xmlDoc.Load(prospectResp);

                 var nodes = xmlDoc.DocumentElement.SelectNodes("/rsp/result/prospect");

                 foreach (XmlNode node in nodes)
                 {
                     impNode = finalXmlDoc.ImportNode(node, true);

                     finalXmlDoc.AppendChild(impNode);
                 }                
                 // EVENTUALLY BREAK LOOP AND EXPORT finalXmlDoc
            }

This is what my searching has gotten me to, but I guessing there is a much easier way to do this. 这就是我要寻找的东西,但是我想有一种更容易的方法。

Create a root element in your finalXML, and append everything to the DocumentElement. 在finalXML中创建一个根元素,并将所有内容附加到DocumentElement。 Just load a simple " 只需加载一个简单的“

var xmlDoc = new XmlDocument();
            var finalXmlDoc = new XMLDocument();
            finalXmlDoc.LoadXml( "<xml/>" );


            while (true)
            {
                 //BUILD URL FOR EACH ITERATION AND DO AN HTTP GET                                       

                 xmlDoc.Load(prospectResp);

                 var nodes = xmlDoc.DocumentElement.SelectNodes("/rsp/result/prospect");

                 foreach (XmlNode node in nodes)
                 {
                     impNode = finalXmlDoc.ImportNode(node, true);

                     finalXmlDoc.DocumentElement.AppendChild(impNode);
                 }                
                 // EVENTUALLY BREAK LOOP AND EXPORT finalXmlDoc
            }

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM