Thursday, January 2, 2014

Exporting data to an XML file




Briefly, eXtensible Markup Language (XML) defines a set of rules for encoding documents
electronically. It allows the creation of all kinds of structured documents to exchange between
systems. In Dynamics AX, XML files are widely used across the application. For example, user
profiles can be exported as XML files. Business data, such as financial statements can also be
exported as eXtensible Business Reporting Language (XBRL) files, which are based on XML.

Dynamics AX also contains a set of application classes prefixed with Xml, such as
XmlDocument and XmlNode. Basically, those classes are wrappers around the System.XML
namespace in the .NET framework.

In this, we will create a new simple XML document by using the latter classes, in order
to show the basics of XML. We will create the file with the data from the chart of the accounts
table and will save it as an XML file.

How to do it....

Carry out the following steps in order to complete this recipe

Open the AOT and create a new class named CreateXmlFile with the following code:

class CreateXmlFile
 {
 }

public static void main(Args _args)
{
         XmlDocument doc;
         XmlElement nodeXml;
         XmlElement nodeTable;
         XmlElement nodeAccount;
         XmlElement nodeName; 
         MainAccount mainAccount;
         #define.filename(@'C:\Temp\accounts.xml')
         doc = XmlDocument::newBlank();
         nodeXml = doc.createElement('xml');
         doc.appendChild(nodeXml);
         while select RecId, MainAccountId, Name from mainAccount
            {
               nodeTable = doc.createElement(tableStr(MainAccount));
               nodeTable.setAttribute(fieldStr(MainAccount, RecId),int642str(mainAccount.RecId));
               nodeXml.appendChild(nodeTable);
               nodeAccount = doc.createElement(
               fieldStr(MainAccount, MainAccountId));
               nodeAccount.appendChild(
              doc.createTextNode(mainAccount.MainAccountId));
              nodeTable.appendChild(nodeAccount);
              nodeName = doc.createElement(
              fieldStr(MainAccount, Name));
              nodeName.appendChild(
             doc.createTextNode(mainAccount.Name));
             nodeTable.appendChild(nodeName);
         }
       doc.save(#filename);
       info(strFmt("File %1 created.", #filename));
}

Run the class. The XML file accounts.xml should be created in the specified folder. Open it using any XML editor or viewer, such as Microsoft Internet Explorer, and review the created XML structure.


Iace technologies & Services..!!





No comments:

Post a Comment