Serialization order in Data Contract

If you have a Data Contract in your WCF service as below,

Student.cs


    [DataContract]
    public class Student
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Address { get; set; }
        [DataMember]
        public string RollNumber { get; set; }
    }

On serialization properties of data contract will get serialized in alphabetical order. So at the client side serialized data contract will look like clip_image002 So we can see in serialized data contract at the client side properties are serialized in alphabetical order. Now if you want to manage the order of serialization, you need to use Order attribute of DataMember. Student.cs



    [DataContract]
    public class Student
    {
        [DataMember(Order=3)]
        public string Name { get; set; }
        [DataMember(Order=2)]
        public string Address { get; set; }
        [DataMember(Order=1)]
        public string RollNumber { get; set; }
    }

}

On serialization properties of data contract will get serialized in order specified at order attribute of data contract.

clip_image004

Note: To see the order of serialization click on ServiceReference1 in object explorer when you are adding that at the client side. You will get a class Student.cs

Two important points

1. If two properties are having same order then they will get serialized in alphabetical order.

2. In inheritance also DataContract will be serialized in alphabetical order , if explicitly order is not specified on DataMember.

Dhananjay Kumar is Developer, Blogger , Speaker, Learner , Mindcracker & Microsoft MVP.

Posted in WCF
One comment on “Serialization order in Data Contract

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

  • after 2 sixes of Sami .. I say LOL -> Virat KOHLI ... happy to see Kohli the ILL ATTITUDE MAN LOOSING .. 4 hours ago
  • It does not matter from where are you coming ? only does matter where are you going ? #djsays 4 hours ago
  • With people who participated in WCF day .. Great hosting by @CsharpCorner amd great leadership of @mcbkruse http://t.co/HkHjCdveJC 5 hours ago
  • RT @petekrueger: I've been using @Icenium. I would definitely recommend checking it out if you're tackling cross-platform mobile: http://t.… 5 hours ago
  • मुझे ग़म है की मैंने जिंदगी में कुछ नहीं पाया , ये ग़म दिल से निकल जाये अगर तुम मिलने आ जाओ 7 hours ago
Categories
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my current or previous employer's view in anyway. © Copyright 2013
Follow

Get every new post delivered to your Inbox.

Join 2,123 other followers

%d bloggers like this: