Read XML file from URL in AX 2012

Continue reading

In this article I would like to show how we can read from XML files.

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. 

More articles
Explore our blog

What can we do for you?

We'd love to hear about your project. Our team will get back to you within two working days.

Thank you for inquiry, we’ve passed it to our sales department, our representative will reach you back in his earliest convenience.
Oops! Something went wrong while submitting the form.