Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

Serialize / deserialize Data Contracts from within AX

Posted on 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
    Sivolution 130 on at
    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
    Martin Dráb 229,297 Most Valuable Professional on at
    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
    Sivolution 130 on at
    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
    Sivolution 130 on at
    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
    Rachit 4,015 User Group Leader on at
    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
    Martin Dráb 229,297 Most Valuable Professional on at
    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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,902 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,297 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans