Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
827 views
in Technique[技术] by (71.8m points)

c# - WCF DataContract DataMember order?

Is the xml that is created from your DataContract created in alphabetical order. I have a DataContract class defined as:

[DataContract(Name = "User", Namespace = "")]
public class User
{
    [DataMember]
    public string FirstName { get; set; }
    [DataMember]
    public string LastName { get; set; }
    [DataMember]
    public string Email { get; set; }
    [DataMember]
    public string Password { get; set; }

}

When I did the following POST:

<User>
   <FirstName>abc</FirstName>
   <LastName>123</LastName>
   <Email>email@email.com</Email>
   <Password>pass</Password>
</User>

When I did a GET after my post and returned the result as JSON, email was null, but if I POST my xml as:

 <User>
   <Email>email@email.com</Email>
   <FirstName>abc</FirstName>
   <LastName>123</LastName>
   <Password>pass</Password>
 </User>

Email is no longer null when I do a GET and return it as JSON. Why is it doing this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

decorate it with the Order Parameter in the DataMemberAttribute class:

[DataMember(Order = index)]

The reflector in the serializer puts it alphabetically. Unless when decorated like this:

[DataMember(Order = 0)]
public string FirstName { get; set; }
[DataMember(Order = 1)]
public string LastName { get; set; }
[DataMember(Order = 2)]
public string Email { get; set; }
[DataMember(Order = 3)]
public string Password { get; set; }

Read more here...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...