web
You’re offline. This is a read only version of the page.
close
Skip to main content
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
I have the same question (0)
  • Martin Dráb Profile Picture
    236,025 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
    236,025 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
    236,025 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

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

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 886 User Group Leader

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 870 Super User 2025 Season 2

#3
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 697

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans