Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Suggested answer

This document already has a 'DocumentElement' node. Error

Posted on by 207
"Hi all,
 
I am trying to generate one xml file. I am having trouble to create more than one document header node. Please see below how it needs to be generated.
 
<?xml version="1.0" encoding="utf-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
<CstmrCdtTrfInitn>
<GrpHdr>
<MsgId>BXXX-012751</MsgId>
 
 
I have written the below code , and getting error "This document already has a 'DocumentElement' node." in CstmrCdtTrfInitn  and GrpHdr node.

            xmlDoc = XMLDocument::newBlank();

            // Create first line containing version info
            rootNode = xmlDoc.documentElement();
           

            docNode = xmlDoc.createElement2('Document',"urn:iso:std:iso:20022:tech:xsd:pain.001.001.03");
            docNode.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            xmlDoc.appendChild(docNode);

           
            firstnode = xmlDoc.createElement('CstmrCdtTrfInitn');
            xmlDoc.appendChild(firstnode);
            secondnode = xmlDoc.createElement('GrpHdr');
            NodeName =  xmlDoc.appendChild(secondnode);
            xmlElement = xmlDoc.createElement('MsgId');
            NodeMsg = NodeName.appendChild(xmlElement);
            xmlText =xmlDoc.createTextNode(numseq);
            NodeMsg.appendChild(xmlText);
 
 
 
Please help.
 
Thanks,
Priya
  • Martin Dráb Profile Picture
    Martin Dráb 228,706 Most Valuable Professional on at
    This document already has a 'DocumentElement' node. Error
    It's good to hear that the problem has been resolved. Please don't forget to mark the helpful reply as the answer.
     
    Regarding your problem with xmlns, you get an empty namespace because you didn't provide any. You seem to want the same namespace as for Document element. If you need more help with it, please create a new thread, because it's not related to the topic of this thread (This document already has a 'DocumentElement' node. Error).
  • PriyaDutta Profile Picture
    PriyaDutta 207 on at
    This document already has a 'DocumentElement' node. Error
    Hi Martin,
     
    Thanks for your suggestion.
    It is working now but there is one issue . Please see below . 
     
     
     
    Not sure why is this coming. Can you please suggest . It should be as below 
     <CstmrCdtTrfInitn>
    Please see the code for reference.
     rootNode = xmlDoc.documentElement();
    
    
     docNode = xmlDoc.createElement2('Document',"urn:iso:std:iso:20022:tech:xsd:pain.001.001.03");
     docNode.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
     xmlDoc.appendChild(docNode);
    
    
     xmlElement = xmlDoc.createElement('CstmrCdtTrfInitn');
     CstmrCdtTrfInitnNode = docNode.appendChild(xmlElement);                        //todo
     xmlElement = xmlDoc.createElement('GrpHdr');
     
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 228,706 Most Valuable Professional on at
    This document already has a 'DocumentElement' node. Error
    You're wrong in thinking that CstmrCdtTrfInitn and GrpHdr are on the same level as Document. The actual structure of your document is following:
    <?xml version="1.0" encoding="utf-8"?>
    <Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
      <CstmrCdtTrfInitn>
        <GrpHdr>
          <MsgId>BXXX-012751</MsgId>
        ...
        </GrpHdr>
      </CstmrCdtTrfInitn>
    </Document>
    Therefore you need to add firstnode as a child of docNode and secondnode as a child of firstnode, not children of xmlDoc. Note that your code will become much easier to follow if you use descriptive names for your variables.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,572 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,706 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans