Paul Michaels
Paul Michaels

Reputation: 16695

Adding an XML document inside another in Dynamics AX

I have a function that builds and returns an XML document. I then want to insert this entire document inside another XML document. The problem that I seem to be having is that the XML header information is automatically added to the XML document, and then I get an error when trying to insert this. My code is as follows:

xmlElem = xmlDoc.createElement("MyNode");

tmpXmlStr = this.MyXmlBuildFunc();
xmlElem.innerXml(tmpXmlStr);

// Now try to add this to the main document
xmlParentNode.appendChild(xmlElem);

My function looks something like this:

str MyXmlBuildFunc()
{
    XmlDocument xmlOut;
    XmlNode curNod;
    XmlElement xmlElem;
    XmlElement xmlParentElem;
    ;

    xmlOut = XmlDocument::newBlank();

    xmlParentElem = xmlOut.createElement("MainNode");

    xmlElem = xmlOut.createElement("NodeName");
    xmlElem.innerText("NodeValue");
    xmlParentElem.appendChild(xmlElem);

    xmlOut.appendChild(xmlParentElem);

    return xmlOut.xml();
}

The error that I get is as follows:

Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it

Tracing it through, I believe this is caused by the XML definition being created by the XmlDocument build up in the function. How can I stop it doing this, ignore it, or get around this error some other way?

Upvotes: 1

Views: 1386

Answers (1)

10p
10p

Reputation: 6706

Try using return xmlParentElem.xml(); instead of return xmlOut.xml();.

Upvotes: 1

Related Questions