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 :
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.

I have the same question (0)
  • Suggested answer
    André Arnaud de Calavon Profile Picture
    300,904 Super User 2025 Season 2 on at

    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.

  • Ameen1623 Profile Picture
    on at

    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

  • ergun sahin Profile Picture
    8,826 Moderator on at

    You can use XmlDocument

    donatas.xyz/read-xml-string.html

    or dataset

  • Ameen1623 Profile Picture
    on at

    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.'

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
#ManoVerse Profile Picture

#ManoVerse 93

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 62 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans