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.
Basically I'm connecting to the Pardot API and requesting ALL prospects. However, the data is paged, so I only get 200 prospects at a time, and they are returned in XML format. 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.
I have something like this, which works upon the first iteration, but during the the next one it fails at the finalXmlDoc.AppendChile(impNode);
with error message: "This document already has a 'DocumentElement' node."
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. 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
}
"