title: Adding an XML document inside another in Dynamics AX
body: <p>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:</p>
  
  <pre><code>xmlElem = xmlDoc.createElement("MyNode");
  
  tmpXmlStr = this.MyXmlBuildFunc();
  xmlElem.innerXml(tmpXmlStr);
  
  // Now try to add this to the main document
  xmlParentNode.appendChild(xmlElem);
  </code></pre>
  
  <p>My function looks something like this:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>The error that I get is as follows:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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?</p>