Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Microsoft Dynamics AX 2012 Development Cookbook

You're reading from   Microsoft Dynamics AX 2012 Development Cookbook Customizing Dynamics AX to suit the specific needs of an organization is plain sailing when you use this cookbook of modifications. With more than 80 practical recipes it's the perfect handbook for all Dynamics AX developers.

Arrow left icon
Product type Paperback
Published in May 2012
Publisher Packt
ISBN-13 9781849684644
Length 372 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Mindaugas Pocius Mindaugas Pocius
Author Profile Icon Mindaugas Pocius
Mindaugas Pocius
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Microsoft Dynamics AX 2012 Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
1. Preface
1. Processing Data FREE CHAPTER 2. Working with Forms 3. Working with Data in Forms 4. Building Lookups 5. Processing Business Tasks 6. Integration with Microsoft Office 7. Using Services 8. Improving Development Efficiency 9. Improving Dynamics AX Performance

Importing data from an XML file


In Dynamics AX, XML file importing is done in a very similar way as exporting. In this recipe, we will continue using the System.XML wrapper application classes. We will create a new class which reads XML files and displays the content onscreen. As a source file, we will use the previously created accounts.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 ReadXmlFile with the following code. Use the document created in the previous recipe:

    class ReadXmlFile
    {
    }
    public static void main(Args _args)
    {
    XmlDocument doc;
    XmlNodeList data;
    XmlElement nodeTable;
    XmlElement nodeAccount;
    XmlElement nodeName;
    #define.filename(@'C:\Temp\accounts.xml')
    doc = XmlDocument::newFile(#filename);
    data = doc.selectNodes('//'+tableStr(MainAccount));
    nodeTable = data.nextNode();
    while (nodeTable)
    {
    nodeAccount = nodeTable.selectSingleNode(
    fieldStr(MainAccount, MainAccountId));
    nodeName = nodeTable.selectSingleNode(
    fieldStr(MainAccount, Name));
    info(strFmt(
    "%1 - %2",
    nodeAccount.text(),
    nodeName.text()));
    nodeTable = data.nextNode();
    }
    }
    
  2. 2. Run the class. The Infolog should display the contents of the accounts.xml file on the screen:

How it works...

In this recipe, we first create a new XmlDocument. We create it from the file and hence we have to use its newFile() method. Then we get all the document nodes of the table as XmlNodeList. We also get its first element by calling the nextNode() method.

Next, we loop through all the list elements and do the following:

  1. 1. Get an account number node as an XmlElement.

  2. 2. Get an account name node as an XmlElement.

  3. 3. Display the text of both nodes in the Infolog.

  4. 4. Get the next list element.

In this way, we retrieve the data from the XML file. A similar approach could be used to read any other XML file.

You have been reading a chapter from
Microsoft Dynamics AX 2012 Development Cookbook
Published in: May 2012
Publisher: Packt
ISBN-13: 9781849684644
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image