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.
Probably, the main thing that is associated with XML in Dynamics AX is the Application Integration Framework. It is an infrastructure that allows exposing business logic or exchanging data with other external systems. The communication is done by using XML formatted documents. By using the existing XML framework application classes prefixed with Axd
, you can export or import data from or to the system in an XML format to be used for communicating with external systems. It is also possible to create new Axd
classes using the AIF Document Service Wizard from the Tools menu to support the export and import of newly created tables.
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 recipe, 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:
1. 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)); }
2. 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:
How it works...
We start the recipe by creating a new XmlDocument
using its newBlank()
method, which represents an XML structure. Then we create its root node named xml
using the createElement()
method, and add the node to the document by calling the document's appendChild()
method.
Next, we go through the MainAccount
table and do the following for each record:
1. Create a new
XmlElement
node, which is named exactly as the table name, and add this node to the root node.2. Create a node representing the account number field and its child node representing its value. The account number node is created using
createElement()
, and its value is created usingcreateTextNode()
. ThecreateTextNode()
method basically adds a value as text with no XML tags.3. Add the account number node to the table node.
4. Create a node representing the account name field and its child node representing its value.
5. Add the account name node to the table node.
Finally, we save the created XML document as a file.
In this way, we can create documents having virtually any structure.