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. 

Zobacz inne

OneLake: czyli jak uporządkować odwieczny chaos w danych

Czytaj artykuł
Text Link

Ręczne przetwarzanie zamówień sprzedażowych to strata czasu. Jak to zautomatyzować?

Czytaj artykuł
Text Link

Najlepszy system ERP na 2026 rok? Microsoft Dynamics 365 w rankingach Gartner Magic Quadrant

Czytaj artykuł
Text Link
Wróć do wszystkich

Porozmawiajmy o Twoim projekcie

Napisz, z czym przychodzisz: wdrożenie, migracja, rozwój systemu albo pytanie, na które szukasz odpowiedzi. Wracamy z konkretną propozycją następnego kroku, nie z prezentacją handlową.

Dziękujemy za wiadomość!
Oops! Something went wrong while submitting the form.
Microsoft AI Builder
Microsoft Copilot Studio
No items found.
No items found.
No items found.