web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

how to do serialization in XML?

(0) ShareShare
ReportReport
Posted on by 465

So i made a contract class

[DataContractAttribute]
class contract
{
    str firstname;
    str lastname;

    [DataMemberAttribute('firstname')]
    public str firstname(str _firstname = firstname)
    {
        firstname = _firstname;
        return firstname;
    }

    [DataMemberAttribute('lastname')]
    public str lastname(str _lastname = lastname)
    {
        lastname = _lastname;
        return lastname;
    }

}

Now i did the following:

contract contract = new contract();
c.firstName("firstname");
c.lastname("lastname");
System.Xml.Serialization.XmlRootAttribute xmlRoot = new System.Xml.Serialization.XmlRootAttribute("root");

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(contract.GetType(),xmlRoot); // I'm getting an error here: There was an error reflecting type 'Dynamics.AX.contract'.  and another error :cannot be serialized because it does not have a parameterless constructor

what should i do to remove this error?

and what do more to be able to get a string of the XML format, there is serializer.serialize(//what to use here)

I have the same question (0)
  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi,

    I have a feeling that you need to mark the class with DataContractAttribute attribute.

  • D365FO user Profile Picture
    465 on at

    I already did that if you scroll the code you'll find it.

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi, 

    Yes, execuse me. 

    Have you checked this article? 

    http://dev.goshoom.net/en/2014/01/data-contract-serialization/

  • D365FO user Profile Picture
    465 on at

    I saw it, but i wanted to write everything in x++ class without creating c# project. Is my approach wrong then? what should i amend in my code to make it work?

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi,

    The main idea is to use DataContractSerializer .net class instead of XmlSerializer. I believe you can write everything in x++ class now (the article is related to AX2012).

  • D365FO user Profile Picture
    465 on at

    can you help a little in the code i wrote how to change it? how can i call dataContractSerializer

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi,

    Try to adjust code from docs and use memory stream instead of filestream.

    If you don't solve it, I can check later if I have some examples. Or someone else could help.

    docs.microsoft.com/.../system.runtime.serialization.datacontractserializer

  • D365FO user Profile Picture
    465 on at

    I read this before but couldn't figure it out so it would be great if you help me

  • Verified answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi,

    Here is an example.

    Contract c = new Contract();
    
    c.firstname("John");
    
    System.Runtime.Serialization.DataContractSerializer s = new System.Runtime.Serialization.DataContractSerializer(c.GetType());
    
    System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
    
    s.WriteObject(memoryStream, c);
    
    memoryStream.Position = 0;
    
    System.IO.StreamReader sr = new System.IO.StreamReader(memoryStream);
    
    str xmlStr = sr.ReadToEnd();
    
    info(xmlStr);

  • D365FO user Profile Picture
    465 on at

    Thanks alooooot Sergie! It worked.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans