Book Image

Microsoft Dynamics AX 2009 Development Cookbook

By : Mindaugas Pocius
Book Image

Microsoft Dynamics AX 2009 Development Cookbook

By: Mindaugas Pocius

Overview of this book

Microsoft Dynamics AX provides a comprehensive Enterprise Resource Planning (ERP) solution for mid-size and larger organizations. Dynamics AX implementations are used worldwide by thousands of customers. With the new version - Dynamics AX 2009 - 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. This step-by-step guide will help you manage your company'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. This book contains commonly used bits of code which were real-world tested in at least one successful Dynamics AX implementation. Many of the recipes were deployed in many implementations and even across several versions of Dynamics AX. The examples 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 book should help developers who already know the basics of Dynamics AX programming to step up to the next level. The recipes 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. Managing your data and functions will become easier. You will also get numerous development tips and tricks from a Dynamics AX development expert.
Table of Contents (12 chapters)
Microsoft Dynamics AX 2009 Development Cookbook
Credits
About the Author
About the Reviewers
Preface
Index

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;
         #define.filename('<documents>\\accounts.xml')
         ;
     
         doc     = XmlDocument::newBlank();
     
         nodeXml = doc.createElement('xml');
     
         doc.appendChild(nodeXml);
     
         while select ledgerTable
         {
             nodeTable = doc.createElement(tablestr(LedgerTable));
     
             nodeTable.setAttribute(
                 fieldstr(LedgerTable, RecId),
                 int642str(ledgerTable.RecId));
     
             nodeXml.appendChild(nodeTable);
     
             nodeAccount = doc.createElement(
                 fieldstr(LedgerTable, AccountNum));
     
             nodeAccount.appendChild(
                 doc.createTextNode(ledgerTable.AccountNum));
     
             nodeTable.appendChild(nodeAccount);
     
             nodeName = doc.createElement(
                 fieldstr(LedgerTable, AccountName));
     
             nodeName.appendChild(
                 doc.createTextNode(ledgerTable.AccountName));
     
             nodeTable.appendChild(nodeName);
         }
     
         doc.save(#filename);
     }
  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:

  • Create a new XmlElement node, which is named exactly as the table name, and add this node to the root node.

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

  • Add the account number node to the table node.

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

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