In Dynamics AX there is no standard feature to serialise/ de-serialise data to XML file based on an XSD schema.
Each time when we want to do this we must create new code and create/read node by node the XML file. So, to make this in dynamic way we can use the Microsoft XSD tool in visual studio to generate serialisation/deserialisation classes and then deploy and use them in Microsoft Dynamics AX.
I will not go in details about creating the serialisation/deserialisation classes because this part is very well explained in many articles about using XSD tool. I will take the example from Yogesh Joshi's Blog and work on top of it to integrate it in Microsoft Dynamics AX 2012. The following prerequisites are needed before starting:
- Create the Students.xsd XML schema file.
- Generate the Students.cs class and deploy the solution to Microsoft Dynamics AX 2012.
Note: In order to deploy the classes to AOT you need to have installed Visual Studio tools for Microsoft Dynamics AX on VS 2010.
Note: Before deployment to AOT make sure that the class is contained in namespace:
By analysing the generated serialisation/deserialisation class we can see that for each XML node one partial class has been created. For repeating nodes two classes are created, one is array of the same class and the other is the standard class with all the get and set methods for node values. We need to declare variables for all these classes in Dynamics AX. I have created a simple for loop with final length of 7 that will insert seven records into XML file:
static void xmlSerialize(Args _args)
System.Xml.XmlWriter xmlWriter = Sys-tem.Xml.XmlWriter::Create(@'C:\Test\StudentsCreated.xml');
XmlSerializationClassLibrary.Students serializedStudents = new XmlSerialization-ClassLibrary.Students();
System.Xml.Serialization.XmlSerializer serializer = new Sys-tem.Xml.Serialization.XmlSerializer(serializedStudents.GetType());
System.String testName, testAddress;
int length = 7;
permission = new InteropPermission(InteropKind::DllInterop);
if (permission == null)
//initialize the array object that will store multiple objects of type StudentsStudent
stud = new XmlSerializationClassLibrary.StudentsStudent[length]();
for (i = 0; i < length; i++)
//initialize StudentsStudent object
studentInserted = new XmlSerializationClassLibrary.StudentsStudent();
//set students name in the StudentsStudent object
testName = strFmt("Name %1", int2str(i));
//set rollid in the StudentsStudent object
testRollId = System.Convert::ToByte(i);
//set address in the StudentsStudent object
testAddress = strFmt("Address %1", int2str(i));
//set StudentsStudent value for the StudentsStudent array object
//set value for the top level node object Students
//call serializer with passing the xmlWriter that contains the file info and serializedStu-dents which contains all data objects
e = CLRInterop::getLastException();
info( e.get_Message() );
e = e.get_InnerException();
In same way we can generate serialisation/deserialisation classes using XSD tool for other XML schemas or if we do not have the XML schema we could create it either automatically again with XSD tool or manually. After that it is very easy to use those classes in Dynamics AX and generate or read XML files with writing little and understandable code.
Programming in Dynamics 365 for Operations
Microsoft Dynamics developer since 2009 (starting from Dynamics AX 2009) with experience in multiple Dynamics implementations across Europe and in almost any technical area of Microsoft Dynamics. Comes from Skopje, Macedonia, Graduated at Technical University in Varna, Bulgaria as Computer Science Engineer. Always supported by his lovely wife and daughter.