Skip to main content

Notifications

Announcements

No record found.

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 333
"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
    234,615 Most Valuable Professional on at
    This document already has a 'DocumentElement' node. Error
    For reference, the question of the namespace is discussed in Extra namespace coming in document node.
  • Verified answer
    Martin Dráb Profile Picture
    234,615 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
    333 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
    234,615 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

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

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

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 565

#2
Martin Dráb Profile Picture

Martin Dráb 536 Most Valuable Professional

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 402 Super User 2025 Season 1

Product updates

Dynamics 365 release plans