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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Read XML through X++ in D365

(0) ShareShare
ReportReport
Posted on by 758

Hi Everyone,

Need urgent help.

I want to read XML though X in D365.

I just want to read 3 values from XML which are highlighted in below screenshot.

Kindly request you to help me on this.

I tried codes from different blogs but did not find any matching XML with my XML.

Please tell me what should I write in my code after validating XML.

public static void readXML(str _xmlMsg)
{
    XmlDocument xmlDoc;
    XMLParseError xmlError;
    
    xmlDoc = new XmlDocument();
    xmlDoc.loadXml(_xmlMsg);
    // Verify XML Document Structure
    xmlError = xmlDoc.parseError();
    if(xmlError && xmlError.errorCode() != 0)
    {
    throw error(strFmt("XML Error: %1", xmlError.reason()));
    }
}

0257.xml.png

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,948 Most Valuable Professional on at

    There are several solutions; this is one of them:

    XmlNamespaceManager nsManager = new XmlNamespaceManager(xmlDoc.nameTable());
    nsManager.AddNamespace('cfdi', 'http://yournamespace');
    nsManager.AddNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
    
    XmlAttribute schemaLocAttrib = xmlDoc.selectSingleNode("//cfdi:Comprobante/@xsi:schemaLocation", nsManager);
    info(schemaLocAttrib.value());

    You just have to replace 'http://yournamespace' with the actual namespace URI (you didn't show it, therefore I could put it there).

    The thing in selectSingleNode() is an XPath query selecting the vale you want.

  • Suggested answer
    Mansour Yahya Mohamad Profile Picture
    on at

    Hi Rhushikesh,

    This blog might be helpful: donatas.xyz/read-xml-string.html

  • Suggested answer
    Rhushikesh R Profile Picture
    758 on at

    Hi everyone,

    I got the solution for this query. Thanks Martin and Mansour for your opinions.

    Below is my code,

    private container readXML(str _xmlMsg)
    
    {
    
        XmlDocument xmlDoc;
        
        XmlNode cfdiComprobanteNode;
        
        XmlNode cfdiEmisorNode;
        
        XmlNode cfdiComplementoNode;
        
        XmlNode tfdTimbreFiscalDigitalNode;
        
        XMLParseError xmlError;
        
        XmlNamedNodeMap mapComprobante;
        
        XmlNamedNodeMap mapEmisor;
        
        XmlNamedNodeMap mapTimbreFiscalDigital;
        
        Amount invoiceAmount;
        
        str rfcVendor;
        
        str UUID;
        
        xmlDoc = new XmlDocument();
        
        xmlDoc.loadXml(_xmlMsg);
        
        xmlError = xmlDoc.parseError();
        
        if(xmlError && xmlError.errorCode() != 0)
        
        {
        
        throw error(strFmt("XML Error: %1", xmlError.reason()));
        
        }
        
        cfdiComprobanteNode = xmlDoc.documentElement();
        
        mapComprobante = cfdiComprobanteNode.attributes();
        
        invoiceAmount = str2Num(mapComprobante.getNamedItem('Total').nodeValue());
        
        cfdiEmisorNode = cfdiComprobanteNode.getNamedElement('cfdi:Emisor');
        
        mapEmisor = cfdiEmisorNode.attributes();
        
        rfcVendor = mapEmisor.getNamedItem('Rfc').nodeValue();
        
        cfdiComplementoNode = cfdiComprobanteNode.getNamedElement('cfdi:Complemento');
        
        tfdTimbreFiscalDigitalNode = cfdiComplementoNode.getNamedElement('tfd:TimbreFiscalDigital');
        
        mapTimbreFiscalDigital = tfdTimbreFiscalDigitalNode.attributes();
        
        UUID = mapTimbreFiscalDigital.getNamedItem('UUID').nodeValue();
        
        if(!invoiceAmount || !rfcVendor || !UUID)
        
        {
        
        throw error("Value for Invoice Amount/Rfc/UUID not found in XML file.");
        
        }
        
        else
        
        {
        
        info(strFmt("XML Data : Total = %1 , Rfc = %2, UUID = %3", invoiceAmount, rfcVendor, UUID));
        
        }
        
        return[invoiceAmount, rfcVendor, UUID];
    
    }

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 559 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans