.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Serialization order in Data Contract in WCF

Posted By:Dhananjay Kumar       Posted Date: October 04, 2010    Points: 50    Category: WCF    URL: http://www.dotnetspark.com  

Serialization order in Data Contract. In this article we will see how to perform Serialization in Data Contract in WCF.
 

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



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.



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.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend