Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

X++ to create XML data to DD365 table

(0) ShareShare
ReportReport
Posted on by

Dear all,

    Can any one help me that how to get the values from XML file in to D365 table using X++ in D365.

  • Ameen1623 Profile Picture
    Ameen1623 on at
    RE: X++ to create XML data to DD365 table

    The data flow getting failure in this line

    xmlError  = doc.parseError();

       if(xmlError && xmlError.errorCode() != 0)

       {

           throw error(strFmt("XML Error: %1", xmlError.reason()));

       }

    May I know what might me the reason? am fetching the data from web URL.

    Here is the full code.

    public static void main(Args _args)

       {

           System.Net.HttpWebResponse response = null;

           System.Net.WebException webException = null;

           CLRObject webResponse = null;

           str errorString;

           URLBase url = "Mr URL";

           const int batchSize = 1024;

           str responseData;

           XMLDocument XMLdoc;

           int i,CountItemtags;

           doc = new XmlDocument();

           try

           {

               System.Net.HttpWebRequest request = null;

               request = System.Net.WebRequest::Create(url) as System.Net.HttpWebRequest;

               // If required by the server, set the credentials.

               //request.Credentials = CredentialCache.DefaultCredentials;

               webResponse = request.GetResponse();

               response = webResponse as System.Net.HttpWebResponse;

           }

           catch (Exception::CLRError)

           {

               System.Exception exception = ClrInterop::getLastException();

               if (exception)

               {

                   CLRObject clrExceptionMessage = exception.get_message();

                   errorString = CLRInterop::getAnyTypeForObject(clrExceptionMessage);

                   exception = exception.get_InnerException();

                   while(exception)

                   {

                       if (exception is System.Net.WebException)

                       {

                           // There is a response body in the answer. This body will be attached in log.

                           webException = exception as System.Net.WebException;

                           webResponse = webException.get_Response();

                           response = webResponse as System.Net.HttpWebResponse;

                       }

                       clrExceptionMessage = exception.get_Message();

                       errorString += "\n" + CLRInterop::getAnyTypeForObject(clrExceptionMessage);

                       exception = exception.get_InnerException();

                   }

               }

           }

           if (response)

           {

               System.IO.Stream receiveStream = response.GetResponseStream();

               str contentEncoding = response.get_ContentEncoding();

               System.Text.Encoding encode;

               if (contentEncoding)

               {

                   encode = System.Text.Encoding::GetEncoding(contentEncoding);

               }

               else

               {

                   encode = new System.Text.UTF8Encoding();

               }

               System.IO.StreamReader readStream = new System.IO.StreamReader(receiveStream, encode);

               System.Char[] read = new System.Char[batchSize]();

               int countRead = readStream.Read(read, 0, batchSize);

               System.Text.StringBuilder sb = new System.Text.StringBuilder();

               System.String readString;

               while (countRead > 0)

               {

                   readString = new System.String(read, 0, countRead);

                   sb.Append(readString);

                   countRead = readStream.Read(read, 0, batchSize);

                   doc.loadxml(sb.ToString());

                   // Verify XML Document Structure

                   xmlError  = doc.parseError();                        --  here itself code getting failed.

                   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()));

                       }

                   }

               }

               readStream.Close();

               responseData = sb.ToString();

               response.Close();

               info(responseData);

           }

       }

    Error Code:

    'XML Error: Exception has been thrown by the target of an invocation.'

  • ergun sahin Profile Picture
    ergun sahin 8,812 Super User 2024 Season 1 on at
    RE: X++ to create XML data to DD365 table

    You can use XmlDocument

    donatas.xyz/read-xml-string.html

    or dataset

  • Ameen1623 Profile Picture
    Ameen1623 on at
    RE: X++ to create XML data to DD365 table

    Dear Andre,

                Thanks for the reply. Am using API to get the data from third party through code Now the data is coming in XML format.  so i have to insert that data in custom staging table

  • Suggested answer
    André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 291,711 Super User 2024 Season 2 on at
    RE: X++ to create XML data to DD365 table

    Hi Ameen,

    You don't need x++ coding directly. You can use the Data Import and Export Framework if the target table is supported with a data entity. If not, you can develop the data entity and map the source file with the data entity fields for import.

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,466 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans