AX – D365FO – Create an XML file in X++

Reference article : https://shyamkannadasan.blogspot.com/2016/06/ax-creates-xml-file-in-x-containing.html

This example create an XML file in X++ containing employee details

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

   permission= new FileIoPermission('C:\\Altitudo\\Empl_Details_List.xml','w');
   permission.assert();
   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());
      NodeName.appendChild(xmlText);
 
      // Create a node for the address
      xmlElement = xmlDoc.createElement('EmplEmail');
      NodeAddr = NodeEmpl.appendChild(xmlElement);
      xmlText =xmlDoc.createTextNode(HCMWorker.email());
      NodeAddr.appendChild(xmlText);
   }

   // Save the file
   xmldoc.save('C:\\Altitudo\\Empl_Details_List.xml');

}

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s