X++ Code to read XML file in D365FO

X++ Code to read XML file in D365FO

Hi Guys,

The below code will help you to read an XML file from Local File Path and insert into Table.
        XmlDocument         xmlDocument;
        XMLParseError       xmlError;
        XmlNode             rootNode,bodyNode;
        XMLNodeList         tagsList;
        XmlNamedNodeMap     map;
        int                 j;
        RecordInsertList    recordInsertListCust;
        CustomerTableTmp    customerTabletmp;
        Str        fileName =@''D:Temp\customer.XML";
             
        xmlDocument = new XmlDocument();
        xmlDocument.load(fileName);
        xmlError  = xmlDocument.parseError();
        if(xmlError && xmlError.errorCode() != 0)
        {
            throw error(strFmt("XML Error: %1", xmlError.reason()));
        }
        rootNode = xmlDocument.documentElement();
        recordInsertListCust = new RecordInsertList(tableNum(CustomerTableTmp));
       if(rootnode.hasChildNodes())
        {
            tagsList = rootnode.childNodes();
            for(j = 0; j < tagsList.length(); j++)
            {
                bodyNode = tagsList.item(j);
                map= bodyNode.attributes();
                customerTabletmp.clear();
                customerTabletmp.Account    = map.nextNode().nodeValue();
                customerTabletmp.Address    = map.nextNode().nodeValue();         
                customerTabletmp.ContactPersonNo= map.nextNode().nodeValue();
                customerTabletmp.Country = map.nextNode().nodeValue();
                customerTabletmp.Currency = map.nextNode().nodeValue();
                customerTabletmp.CustomerGroup =map.nextNode().nodeValue();
                customerTabletmp.Discount =map.nextNode().nodeValue();
                customerTabletmp.Email =map.nextNode().nodeValue();
                customerTabletmp.GSTNo =map.nextNode().nodeValue();
                customerTabletmp.GSTType =map.nextNode().nodeValue();
                customerTabletmp.Name = map.nextNode().nodeValue();
                customerTabletmp.PANNumber =map.nextNode().nodeValue();
                customerTabletmp.State =map.nextNode().nodeValue();
                customerTabletmp.Imported = NoYes::No;
                recordInsertListCust.add(customerTabletmp);
            }
            recordInsertListCust.insertDatabase();
        }

Limitation:The way XML attribute and values are arranged in XML file,in the same order we need to insert to table. 

ThankYou.







Comments

Popular posts from this blog

x++ code to add hyperlink to a field in D365FO

x++ code to get Vendor Email Address based on Purpose in Dynamics 365 for Operations

x++ code to get Customer Address based on purpose in ax 2012