Thursday, 20 February 2014

How to create XML file through code in Ax 2012

static void ExportXML(Args _args)
{
    XmlDocument     doc;
    XmlElement      nodeXml;
    XmlElement      nodeTable;
    XmlElement      nodeAccount;
    XmlElement      nodeName;
    XmlFile            xmlFile; // Table Name

    #define.filename(@' D:\XMLFile.xml')

    doc = XmlDocument::newBlank();
    nodeXml = doc.createElement('xml');
    doc.appendChild(nodeXml);
    while select RecId,Id, Name,Phone from xmlFile
    {
    nodeTable = doc.createElement(tableStr(XmlFile));
    nodeTable.setAttribute(fieldStr(XmlFile, RecId),
    int642str(xmlFile.RecId));
    nodeXml.appendChild(nodeTable);
    nodeAccount = doc.createElement(fieldStr(XmlFile, Id));
    nodeAccount.appendChild(doc.createTextNode(xmlFile.Id));
    nodeTable.appendChild(nodeAccount);
    nodeName = doc.createElement(fieldStr(XmlFile, Name));
    nodeName.appendChild(doc.createTextNode(xmlFile.Name));
    nodeTable.appendChild(nodeName);
    nodePhone = doc.createElement(fieldStr(XmlFile, Phone));
    nodePhone.appendChild(doc.createTextNode(xmlFile.Phone));
    nodeTable.appendChild(nodePhone);
    }
    doc.save(#filename);
    info(strFmt("File %1 created.", #filename));
    }

No comments:

Post a Comment