No items found.

Read XML file from URL in AX 2012

I hope that after this short presentation you will understand how it works and how to implement this functionality in your future projects.

Below there is an example of XML file:

‍

Example of XML file

For this purpose I’ve wrote a short Job to display data on a screen:

static void ReadFromXMLfile(Args _args)

{

   System.Net.WebResponse              webResponse;

   System.Net.HttpWebRequest           httpWebRequest;

   System.Net.WebHeaderCollection      webHeaderCollection;

   System.IO.StreamReader              streamReader;

   System.IO.Stream                    stream;

   str                                 xmlOut;

   XmlDocument                         xmlDocument;

   XmlNodeList                         xmlNodeList;

   XmlNode                             mainNode;

   XmlNode                             childNode;

   #define.ServiceTimeout(5000)

   #define.ServiceURL('http://www.w3schools.com/xml/simple.xml')

   #define.mainNodeName('breakfast_menu')

   #define.childNodeName('food')

   #define.nameNode('name')

   #define.priceNode('price')

   #define.descriptionNode('description')

   #define.caloriesNode('calories')

   #define.HttpWebRequestMethod("GET")

   #define.HttpWebRequestContentType("application/xml")

   #define.HttpHeaderAuthorization("Authorization")

   httpWebRequest          = System.Net.WebRequest::CreateHttp(#ServiceURL);

   httpWebRequest.set_Method(#HttpWebRequestMethod);

   httpWebRequest.set_ContentType(#HttpWebRequestContentType);

   httpWebRequest.set_Timeout(#ServiceTimeout);

   webHeaderCollection     = httpWebRequest.get_Headers();

   webResponse             = httpWebRequest.GetResponse();

   stream                  = webResponse.GetResponseStream();

   streamReader            = new System.IO.StreamReader(stream);

   xmlOut                  = streamReader.ReadToEnd();

   // --------------------- READ FROM XML-----------------

   xmlDocument             = XmlDocument::newXml(XMLOut);

   mainNode                = xmlDocument.selectSingleNode(#mainNodeName);

   xmlNodeList             = mainNode.selectNodes(#childNodeName);

   childNode               = xmlNodeList.nextNode();

   while (childNode)

   {

       info ( childNode.selectSingleNode(#nameNode).text() +  "; Price:  " + childNode.selectSingleNode(#priceNode).text() +  ";  " +

              childNode.selectSingleNode(#descriptionNode).text() +  "; Calories: " + childNode.selectSingleNode(#caloriesNode).text());

       childNode    = xmlNodeList.nextNode();

   }

   // --------------------- READ FROM XML-----------------

}

At the beginning I am using HttpWebRequest  methods to implement a new instance for the specified URL. After that I am setting the attributes for this connection. Next step is getting the collection of headers name/value pairs associated with the request.

When the connection is working properly we can start to read the XML file. I am using System.IO.Stream and System.IO.StreamReader classes for this.

In my case I am using StreamReader.ReadToEnd() method to read the whole XML file and save it as a string. You can also read line by line using StreamReader.ReadLine() method. Here is a quick example of it:

static void ReadFromURL_example(Args _args)

{

   System.Net.WebClient        webClient;

   System.IO.Stream            stream;

   System.IO.StreamReader      streamReader;

   System.String               line;

   #define.ServiceURL('http://www.w3schools.com/xml/simple.xml')

   webClient           = new System.Net.WebClient();

   stream              = webClient.OpenRead(#ServiceURL);

   streamReader        = new System.IO.StreamReader(stream);

   line                = streamReader.ReadLine();

   while (!System.String::IsNullOrEmpty(line))

   {

       info(line);

       line             = streamReader.ReadLine();

   }

}

At the end there is part of a code which is responsible for reading from XML. We have to specify the main node and the child nodes. At the beginning of this article I’ve marked those variables at the screenshot. 

‍

See more

Migration

SAP ECC to Dynamics 365 migration for Irish companies: timeline and cost in EUR

Read article
Text Link
Managed services

Who can take over Dynamics 365 Finance and Operations application management for a company in Sweden?

Read article
Text Link
Migration

Dynamics AX 2012 to Dynamics 365 migration in Norway: timeline and cost

Read article
Text Link
Back to all

Looking for a Microsoft partner? Let's talk about your project

Work with a team that has delivered implementations and projects for dozens of companies. Schedule an introductory call and see:

How to tailor Dynamics 365 to your company's processes, rather than the other way around

Which areas are worth automating to save time and reduce costs

Here is the step-by-step implementation process with Anegis: from analysis to post-launch support

Select a meeting time
Thank you for your message!
Oops! Something went wrong while submitting the form.
Microsoft AI Builder
Microsoft Copilot Studio
No items found.
No items found.
No items found.