Create and read XML using XSD tool in AX 2012

Create and read XML using XSD tool in AX 2012

Tutorial
Published on
Sep 3, 2015

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:

  1. Create the Students.xsd XML schema file.
  2. 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();

   XmlSerializationClassLibrary.StudentsStudent    studentInserted;

   XmlSerializationClassLibrary.StudentsStudent[]  stud;

   System.Xml.Serialization.XmlSerializer          serializer = new Sys-tem.Xml.Serialization.XmlSerializer(serializedStudents.GetType());

   System.String                                   testName, testAddress;

   System.Byte                                     testRollId;

   System.Exception                                e;

   InteropPermission                               permission;

   int                                             i;

   int                                             length = 7;

   try

     {

       permission = new InteropPermission(InteropKind::DllInterop);

       if (permission == null)

       {

           return;

       }

       permission.assert();

       //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));

           studentInserted.set_Name(testName);

           //set rollid in the StudentsStudent object

           testRollId = System.Convert::ToByte(i);

           studentInserted.set_RollNo(testRollId);

           //set address in the StudentsStudent object

           testAddress = strFmt("Address %1", int2str(i));

           studentInserted.set_Address(testAddress);

           //set StudentsStudent value for the StudentsStudent array object

           stud.SetValue(studentInserted, i);

       }

       //set value for the top level node object Students

       serializedStudents.set_Student(stud);

       //call serializer with passing the xmlWriter that contains the file info and serializedStu-dents which contains all data objects

       serializer.Serialize(xmlWriter, serializedStudents);

       CodeAccessPermission::revertAssert();

    }

    catch(Exception::CLRError)

   {

       e = CLRInterop::getLastException();

       while (e)

       {

           info( e.get_Message() );

           e = e.get_InnerException();

       }

       throw error(CLRInterop::getLastException());

   }

}

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.

Happy DAXing.

Share it
New course!

Programming in Dynamics 365 for Operations

19-23 June 2017
Wroclaw, Poland
In-depth Dynamics 365 for Operations training course is now available in Poland!
Read more →
Written by
Viktor Ristkovski

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.