Book Image

Microsoft Dynamics AX 2012 Development Cookbook

By : Mindaugas Pocius
Book Image

Microsoft Dynamics AX 2012 Development Cookbook

By: Mindaugas Pocius

Overview of this book

Microsoft Dynamics AX is a comprehensive Enterprise Resource Planning (ERP) solution for mid-size and large organizations. Dynamics AX implementations are used worldwide by thousands of customers. With the new version - Dynamics AX 2012 - the system is due to expand even more rapidly. Every new implementation requires some level of customization, and all organizations want this to be done to the highest standards using proven approaches. Written by one of the leading experts in Microsoft Dynamics AX, 'Microsoft Dynamics AX 2012 Development Cookbook' is packed with over 80 task-based and immediately reusable recipes that will help you manage your company's or customer's ERP information and operations efficiently, and solve your business process problems in an effective and quick way. This book focuses on commonly used custom modifications in major Dynamics AX modules. The recipes in this book cover various areas of Dynamics AX to help developers not only learn about programming, but also about the functional side of Dynamics AX. The practical recipes will also allow you to look at the development from the perspective of business processes. You will learn to enhance your user interface using various Dynamics AX UI elements and managing your data and functions will become easier.
Table of Contents (15 chapters)
Microsoft Dynamics AX 2012 Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

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. 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. 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. 1. Create a new XmlElement node, which is named exactly as the table name, and add this node to the root node.

  2. 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 using createTextNode(). The createTextNode() method basically adds a value as text with no XML tags.

  3. 3. Add the account number node to the table node.

  4. 4. Create a node representing the account name field and its child node representing its value.

  5. 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.