Skip to main content
Post a question

Notifications

Community site session details

Community site session details

Session Id : u/PXIwU9nBZOlGo1uGwUkJ
Microsoft Dynamics AX (Archived)

Serialize / deserialize Data Contracts from within AX

Like (0) ShareShare
ReportReport
Posted on 21 Jan 2014 12:38:38 by 130

Hi, I've defined some data contract classes in AX (through the DataContractAttribute) and now I would like to serialize / deserialize these to and from an xml-string. From within AX (x++). Should be fairly easy, but I don't seem to find the code that does the trick.

Anyone any suggestions ?

Tx !!

*This post is locked for comments

  • Sivolution Profile Picture
    130 on 04 Feb 2014 at 11:31:48
    RE: Serialize / deserialize Data Contracts from within AX

    Hi Martin,

    This is great, you got this thing to work ! I knew it should be possible, but I didn't get there... I have to admit, I eventually started writing a serializer in x++ (which was not that hard after all), but this is a way better solution.

    Took me a while to answer your comment, since I wanted to verify the solution. But it works like a charm.

    Tx !

  • Verified answer
    Martin Dráb Profile Picture
    232,877 Most Valuable Professional on 27 Jan 2014 at 05:50:53
    RE: Serialize / deserialize Data Contracts from within AX

    I think that looking for "a piece of existing code" is a wrong approach here. You should be rather prepared to do some research and development.

    Nevertheless find a solution in Data contract serialization from X++.

  • Sivolution Profile Picture
    130 on 22 Jan 2014 at 11:30:42
    RE: Serialize / deserialize Data Contracts from within AX

    I have no controller involved in my process, but if I understand your suggestion correctly, you suggest to fill the data contract from code, but that's actually the same as writing your own deserializer.

    I'm looking for a piece of 'existing' code in which I can pass any XML and I expect it to fill any DataContract.

  • Sivolution Profile Picture
    130 on 22 Jan 2014 at 11:26:40
    RE: Serialize / deserialize Data Contracts from within AX

    Code should then look someting like this:    

    AifCurrencyInfo dc = new AifCurrencyInfo();

    System.Runtime.Serialization.DataContractSerializer ser;

    System.Type dcType;

    dc.parmCurrencyCode("EUR");

    dcType = CLRInterop::getType('Dynamics.Ax.Application.AifCurrencyInfo');

    ser =  new System.Runtime.Serialization.DataContractSerializer(dcType);

    ser.WriteObject(writer, dc);

    writer.Close();

    But unfortunately doesn't work :)

    'Dynamics.Ax.Application.AifCurrencyInfo' is not known in x++. CIL does not recognize it either. Adding the Dynamics.Ax.Application.dll to the bin-dir doesn't seem like a good solution

    and even then, how to convert from AifCurrencyInfo to 'Dynamics.Ax.Application.AifCurrencyInfo' ? Although that might be automatically, once in CIL ...

  • Rachit Profile Picture
    4,015 User Group Leader on 22 Jan 2014 at 02:39:30
    RE: Serialize / deserialize Data Contracts from within AX

    Hi,

    Do you have a controller class also playing some role in the process? You can override the prePromptModifyContract method of your controller class to read the XML and set the values of the data contract members.

    I am not sure if I understood your problem correctly.

    Thanks,

    Rachit Garg

  • Martin Dráb Profile Picture
    232,877 Most Valuable Professional on 21 Jan 2014 at 18:12:12
    RE: Serialize / deserialize Data Contracts from within AX

    I suspect that AX actually never uses that from X++. When a data contract class is compiled to CIL, DataContractAttribute defined in X++ is converted to System.Runtime.Serialization.DataContractAttribute. This allows AX components (such as the ones integrating AX with WCF) to use the usual .NET DataContractSerializer. I would utilize the same approach, although doing it from X++ may be a bit awkward, but still better than writing your own serializer.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,137 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,877 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Loading started