web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

This document already has a 'DocumentElement' node. Error

(2) ShareShare
ReportReport
Posted on by 351
"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
I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    238,959 Most Valuable Professional on at
    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.
  • PriyaDutta Profile Picture
    351 on at
    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');
     
  • Verified answer
    Martin Dráb Profile Picture
    238,959 Most Valuable Professional on at
    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).
  • Martin Dráb Profile Picture
    238,959 Most Valuable Professional on at
    For reference, the question of the namespace is discussed in Extra namespace coming in document node.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 573

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 573

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 396 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans