Export data to XML from AX Tables

Below code snippet is to export the data in tables to XML format:

class ExportToXML
{
}
   public static void main(Args _args)
{
    XmlDocument doc;
    XmlElement nodeXml;
    XmlElement nodeTable;
    XmlElement nodeAccountId;
    XmlElement nodeName;
    Table6 tab;

    #define.filename(@'C:\Temp\TestXML.xml')

    doc = XmlDocument::newBlank();
    nodeXml = doc.createElement('xml');
    doc.appendChild(nodeXml);

    while select RecId, Name,Id from tab
    {

        nodeTable = doc.createElement(tableStr(Table6));
        nodeTable.setAttribute(fieldStr(Table6, RecId),int642str(tab.RecId));
        nodeXml.appendChild(nodeTable);

        nodeAccountId = doc.createElement(fieldStr(Table6, Id));
        nodeAccountId.appendChild(doc.createTextNode(tab.Id));
        nodeTable.appendChild(nodeAccountId);

        nodeName = doc.createElement(fieldStr(Table6, Name));
        nodeName.appendChild(doc.createTextNode(tab.Name));
        nodeTable.appendChild(nodeName);
    }

    doc.save(#filename);
    info(strFmt("File %1 created.", #filename));
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s