AX / D365FO – Create an xml file and download into the browser with File::SendFileToUser #d365fo #ax #msdyn365fo

To create an xml file you can use XmlNode class and if you want to save it into a folder you can use save method, but if you want to send to the user using the browser download functionality you have to use File::SendFileToUser method.

This method accepts only a MemoryStream so you must convert the xml to this data type before to use it.

This code shows how to do it

class XMLWriteEmplAddr
    public static void main(Args _args)
        XMLDocument xmlDoc = XMLDocument::newBlank();
        XMLNode rootNode;
        XMLNode NodeEmpl, NodeName, NodeAddr;
        XMLElement xmlElement;
        XMLText xmlText;
        HCMWorker HCMWorker;

        xmlDoc = XMLDocument::newBlank();

        // Create first line containing version info
        rootNode = xmlDoc.documentElement();
        xmlElement = xmlDoc.createElement('EmployeeList');
        rootNode = xmlDoc.appendChild(xmlElement);

        while select HCMWorker
            // Create a node for the Employee record
            xmlElement = xmlDoc.createElement('Employee');
            NodeEmpl = rootNode.appendChild(xmlElement);
            // Create a node for the name
            xmlElement = xmlDoc.createElement('EmplName');
            NodeName = NodeEmpl.appendChild(xmlElement);
            xmlText =xmlDoc.createTextNode(HCMWorker.Name());
            // Create a node for the address
            xmlElement = xmlDoc.createElement('EmplEmail');
            NodeAddr = NodeEmpl.appendChild(xmlElement);
            xmlText = xmlDoc.createTextNode(;

        // Save the file


    public static void writeXmlFile(XmlDocument _xmlDoc)
        using (var stream = new System.IO.MemoryStream())
            XMLWriter writer = XMLWriter::newStream(stream); //Convert the Xml to a memory stream and pass it to use File::SendFileToUser method

            File::SendFileToUser(stream, 'XmlFile.xml');


Xml file will be dowloaded into the browser

Leave a Reply

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

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

Facebook photo

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

Connecting to %s