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, ...
Suggested answer

How to Read XML using x++ code in AX 2012

(0) ShareShare
ReportReport
Posted on by 15

?xml version="1.0" encoding="utf-8"?>
<ns0:Documents
xmlns:ns0="D365InvoiceIngoing">
<ns0:Document DocumentReference="" ImageReference="" CertificateReference="" Version="1.0" Name="XML Invoice" Type="Invoice">
<ns0:Invoice>
<ns0:Supplier>
<ns0:Number>200003</ns0:Number>
</ns0:Supplier>
<ns0:Buyer>
<ns0:CorporateGroupNumber>BOC</ns0:CorporateGroupNumber>
</ns0:Buyer>
<ns0:Fields>
<ns0:Field Name="Faktura/Kredit" Type="invoicecredit">Invoice</ns0:Field>
<ns0:Field Name="Fakturanummer" Type="invoicenumber">PSI871887</ns0:Field>
<ns0:Field Name="Fakturadato" Type="invoicedate">20210326</ns0:Field>
<ns0:Field Name="Momsbeløb" Type="invoicetotalvatamount">0.00</ns0:Field>
<ns0:Field Name="Totalbeløb" Type="invoicetotalvatincludedamount">401.60</ns0:Field>
<ns0:Field Name="Girostreng" Type="paymentreferencenumber"></ns0:Field>
<ns0:Field Name="Valuta" Type="invoicecurrency"></ns0:Field>
<ns0:Field Name="Ordrenummer" Type="invoiceordernumber">PO2167913</ns0:Field>
<ns0:Field Name="Rekvirent" Type="buyercontactpersonname"></ns0:Field>
<ns0:Field Name="FSCCert" Type="FSCCert"></ns0:Field>
</ns0:Fields>
<ns0:Tables />
<ns0:Attachments />
<ns0:InvoiceLines>
<ns0:InvoiceLine>
<ns0:Field Name="Varenummer" Type="ItemNumber">ABCD</ns0:Field>
<ns0:Field Name="Antal" Type="Quantity">15</ns0:Field>
<ns0:Field Name="FSCClaim" Type="FSCClaimValue"></ns0:Field>
</ns0:InvoiceLine>
<ns0:InvoiceLine>
<ns0:Field Name="Varenummer" Type="ItemNumber">BCDE</ns0:Field>
<ns0:Field Name="Antal" Type="Quantity">10</ns0:Field>
<ns0:Field Name="FSCClaim" Type="FSCClaimValue"></ns0:Field>
</ns0:InvoiceLine>
<ns0:InvoiceLine>
<ns0:Field Name="Varenummer" Type="ItemNumber">ABCDE</ns0:Field>
<ns0:Field Name="Antal" Type="Quantity">10</ns0:Field>
<ns0:Field Name="FSCClaim" Type="FSCClaimValue"></ns0:Field>
</ns0:InvoiceLine>
</ns0:InvoiceLines>
</ns0:Invoice>
<ns0:System>
<ns0:Field Type="Pages">\\d365iwfprodstorage.file.core.windows.net\d365iwfprod\read\xml\PSI871487.pdf</ns0:Field>
<ns0:Field Type="DateTime">20210410 00:03:49</ns0:Field>
<ns0:Field Type="Currency"></ns0:Field>
<ns0:Field Type="Originator">Biztalk</ns0:Field>
<ns0:Field Type="ImageFile1">\\d365iwfprodstorage.file.core.windows.net\d365iwfprod\read\xml\PSI871487.pdf</ns0:Field>
</ns0:System>
<ns0:ProcessLog>
<ns0:ProcessMessage>
<ns0:TimeStamp>2021-04-10T00:03:49</ns0:TimeStamp>
<ns0:Type>System</ns0:Type>
<ns0:Module>Verify</ns0:Module>
<ns0:Action>Approved</ns0:Action>
<ns0:Owner>administrator</ns0:Owner>
<ns0:sMessage></ns0:sMessage>
</ns0:ProcessMessage>
</ns0:ProcessLog>
</ns0:Document>
</ns0:Documents>

trying to read above XML using X++ jobs but getting error 

static void readXMLString_Copy(Args _args)
{
// Define XML Document and its nodes
XmlDocument doc,Doc1;
XmlNodeList xmlScriptList,XmlNodeListOne;
XmlNodeList xmlProjectList;
XmlElement nodeScript;
XmlElement nodeProject;
XmlElement nodeNote;
XMLParseError xmlError;
XmlElement firstName;
// Define temporary variables
str _xmlMsg;
int i, j;
str 10000 note;
#define.filename(@'C:\AIF\XML\InvoiceTestSingleLineWithFSC.xml');


Doc1 = XmlDocument::newFile(#filename);
note = Doc1.toString();
// Create the XML Document
doc = new XmlDocument();
doc.loadXml(note);

//doc = XmlDocument::newXml(#filename);
// Verify XML Document Structure
xmlError = doc.parseError();
if(xmlError && xmlError.errorCode() != 0)
{
throw error(strFmt("XML Error: %1", xmlError.reason()));
}

// Get the root element and its child nodes
// xmlScriptList = doc.getElementsByTagName("ns0:Invoice");
// Get the root element and its child nodes
// nodeScript = doc.getNamedElement("ns0:Document");
xmlScriptList = doc.selectNodes("Invoice");
//xmlScriptList = nodeScript.childNodes();

//xmlScriptList = nodeScript.childNodes();

for(i=0; i < xmlScriptList.length(); i++)
{
nodeProject = xmlScriptList.item(i);
xmlProjectList = nodeProject.childNodes();

info("### Project information: ");
info(strFmt("ItemNumber: %1", nodeScript.getAttribute("ItemNumber")));
/*firstName = nodeScript.getAttribute("Fakturanummer");
if (firstName)
{
info(strFmt("First Name: %1", firstName.text()));
}*/

//info(strFmt("ItemNumber: %1", nodeProject.getAttribute("ItemNumber")));
//info(strFmt("To: %1", nodeProject.getElementsByTagName("ItemNumber").text()));

// Loop through the repeating nodes
//info("### Notes: ");
for (j=2; j < xmlProjectList.length(); j++)
{
nodeNote = xmlProjectList.item(j);

info(strFmt("ItemNumber: %1", nodeNote.getAttribute("Varenummer")));
info(strFmt("To: %1", nodeNote.getNamedElement("Varenummer").text()));
//info(strFmt("Body: %1", nodeNote.getNamedElement("Body").text()));
}
}
}

any suggestion will great help. 

I have the same question (0)
  • Suggested answer
    Hossein.K Profile Picture
    6,648 on at
    RE: How to Read XML using x++ code in AX 2012

    Hi,

    These links are useful for you Reading XML Nodes under specific tag through X++ and How to import and export the xml file and Read XML through X++ error

    You can check above

  • Suggested answer
    Hossein.K Profile Picture
    6,648 on at
    RE: How to Read XML using x++ code in AX 2012

    Hi,

    The function to do it

    static void readXMLString(Args _args)
    {
        // Define XML Document and its nodes
        XmlDocument     doc;
        XmlNodeList     xmlScriptList;
        XmlNodeList     xmlProjectList;
        XmlElement      nodeScript;
        XmlElement      nodeProject;
        XmlElement      nodeNote;
        XMLParseError   xmlError;
        // Define temporary variables
        str _xmlMsg;
        int i, j;
    
        ;
    
        // XML string to read
        _xmlMsg = "C:\NotesDesign\Note.jpgPDFPrinterTimMake sure all tasks are completed before holidays!SophieDon't forget your Christmas shopping this weekend!";
    
        // Create the XML Document
        doc = new XmlDocument();
        doc.loadXml(_xmlMsg);
    
        // Verify XML Document Structure
        xmlError  = doc.parseError();
        if(xmlError && xmlError.errorCode() != 0)
        {
            throw error(strFmt("XML Error: %1", xmlError.reason()));
        }
    
        // Get the root element and its child nodes
        nodeScript = doc.getNamedElement("XMLScript");
        xmlScriptList = nodeScript.childNodes();
    
        for(i=0; i < xmlScriptList.length(); i  )
        {
            nodeProject = xmlScriptList.item(i);
            xmlProjectList = nodeProject.childNodes();
    
            info("### Project information: ");
    
            // Print out node and attribute values
            info(strFmt("XML Document Version: %1", nodeScript.getAttribute("Version")));
            info(strFmt("Project Name: %1", nodeProject.getAttribute("Name")));
            info(strFmt("Format: %1", nodeProject.getNamedElement("Format").text()));
            info(strFmt("PrintSetup (innerXML): %1", nodeProject.getNamedElement("PrintSetup").innerXml()));
            info(strFmt("Printer (text): %1 OR Printer (innerText): %2", 
                        nodeProject.getNamedElement("PrintSetup").getNamedElement("Printer").text(), 
                        nodeProject.getNamedElement("PrintSetup").innerText()));
    
            // Loop through the repeating nodes
            info("### Notes: ");       
            for (j=2; j < xmlProjectList.length(); j  )
            {
                nodeNote = xmlProjectList.item(j);
    
                info(strFmt("ID: %1, Date: %2", nodeNote.getAttribute("ID"), nodeNote.getAttribute("Date")));
                info(strFmt("To: %1", nodeNote.getNamedElement("To").text()));
                info(strFmt("Body: %1", nodeNote.getNamedElement("Body").text()));
            }
        }    
    }

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 2,239

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 604 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans