다음을 통해 공유


Putting Data in Order

How do I control the order in which members of the data contract are serialized?

The DataMember attribute has a property called Order that influences the order in which the different members are serialized. Let's call the value of the Order property the data member's order and try to define what the overall ordering looks like. Here are the rules of that ordering.

The data members of a particular type are serialized in increasing order. A data member without a value set for the Order property has an order that is less than the order of any data member with a value set for the Order property. If two or more data members have the same order, then they are ordered according to an ordinal comparison of the names of the data members.

Now, the ordering of data members for a type hierarchy is that data members for a less derived type are ordered before data members for a more derived type.

Therefore, the overall order looks like this.

1.
Data members for less derived types, with this definition applied recursively
2.
Data members without a value set for the Order property, sorted according to an ordinal comparison
3.
Data members with a value set for the Order property, in increasing order, with ties broken according to an ordinal comparison
4.
Data members for more derived types, with this definition applied recursively