Exporting to an XML file

Briefly, Extensible Markup Language (XML) defines a set of rules for encoding documents
electronically. It allows creating of all kind of structured documents. In Dynamics AX, XML
files are widely used across the application. For example, user profiles can be exported as
XML files. Business data like financial statements can also be exported as XBRL (eXtensible
Business Reporting Language) 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 existing XML framework application classes prefixed with
Axd, you can export or import data from or to the system in an XML format. It is also possible
to create new Axd classes using the AIF Document Service Wizard from the Tools |
Development tools | Wizards menu to support exporting and importing newly created tables.
Dynamics AX also contains a set of application classes prefixed with Xml like XmlDocument,
and XmlNode. Basically, those classes are wrappers around the System.XML namespace in
.NET Framework.
In this recipe to show the principle of XML, we will create a new simple XML document by
using the latter classes. We will create the file with the data from the chart of accounts table
and will save it as an XML file.
How to do it…
1. Open AOT and create a new class called CreateXmlFile with the following code.
Replace <documents> with your own path (use double backslashes for folder
separation, that is, \\):
class CreateXmlFile
public static void main(Args _args)
XmlDocument doc;
XmlElement nodeXml;
XmlElement nodeTable;
XmlElement nodeAccount;
XmlElement nodeName;
LedgerTable ledgerTable;
doc = XmlDocument::newBlank();
nodeXml = doc.createElement(‘xml’);

while select ledgerTable
nodeTable = doc.createElement(tablestr(LedgerTable));
fieldstr(LedgerTable, RecId),
nodeAccount = doc.createElement(
fieldstr(LedgerTable, AccountNum));
nodeName = doc.createElement(
fieldstr(LedgerTable, AccountName));
2. Run the class. The XML file accounts.xml should be created in the specified folder.
Open it using Internet Explorer, and review the created XML structure

How it works…
We start by creating a new XmlDocument, which represents an XML structure using its
newBlank() method. Then we create its root node named xml using createElement(),
and add the node to the document by calling the document’s appendChild() method.
Next, we go though the LedgerTable table and do the following for each record:
ff Create a new XmlElement node, which is named exactly as the table name, and add
this node to the root node.
ff 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 using createTextNode(). The createTextNode() method
basically adds a value as text with no XML tags.
ff Add the account number node to the table node.
ff Create a node representing the account name field and its child node representing
its value.
ff Add the account name node to the table node.
Finally, we save the created XML document as a file.
In this way, we could create documents having virtually any structure.


Microsoft Dynamics AX 2009 Development Cookbook Dec 2009

  1. No comments yet.

  1. No trackbacks yet.