0

When I copy the DataColumns class to the RatesOfPay class, the DataMember attributes in WCF are no longer honored when I use the following code to copy. I want to see results in the order Step8,Step9, Step10 but I get Step10, Step8, Step9 as Order atribute is being ignored and columns are getting sorted alphabetically. . Also the EmitDefaultValue = false attribute is getting ignored and I see steps with null values.

List<DataColumns> source = new List<DataColumns>();
List<RatesOfPay> destination = new List<RatesOfPay>();

destination = source.Select(d => new RatesOfPay
{
    step8 = d.column8,
    step9= d.column9,
    step10 = d.column10
}).ToList();

public class DataColumns
{

    [DataMember(Order = 0, Name = "column8", EmitDefaultValue = false)]
    public string column8 { get; set; }

    [DataMember(Order = 1, Name = "column9", EmitDefaultValue = false)]
    public string column9 { get; set; }

    [DataMember(Order = 2, Name = "column10", EmitDefaultValue = false)]
    public string column10 { get; set; }
}

public class RatesOfPay
{

    [DataMember(Order = 0, Name = "Step8", EmitDefaultValue = false)]
    public string step8 { get; set; }

    [DataMember(Order = 1, Name = "Step9", EmitDefaultValue = false)]
    public string step9 { get; set; }

    [DataMember(Order = 2, Name = "Step10", EmitDefaultValue = false)]
    public string step10 { get; set; }
}
Tim
  • 28,212
  • 8
  • 63
  • 76
Raj
  • 1
  • I think you misunderstand what `DataMember` is for. It's not for ordering the properites/fields in a user defined object. It's used to provide information about the member for use by the `DataContractSerializer`. If you look at the serialized result (i.e., XML) you should see Step8, Step9, and Step10. If you look at in the debugger when it's deserialized, you'll see it in ASCII alphabetical order. – Tim Mar 17 '15 at 00:39
  • Likewise, `EmitDefaultValue` directs the serializer to emit the default value for the data type. It is true by default. When you set it to false, any property that has a null value (or default value for value types) will not be serialized, but it will still be in the object. Take a look at [DataMemberAttribute Class](https://msdn.microsoft.com/en-us/library/System.Runtime.Serialization.DataMemberAttribute(v=vs.110).aspx) on MSDN - there are several examples that demonstrate what is what - but this all applies to **serialization**. – Tim Mar 17 '15 at 00:42
  • Thank you very much for your comment. Changing it to use [XmlSerializerFormat] did the trick! – Raj Mar 24 '15 at 15:21

1 Answers1

1

To expand upon my comments, DataMember is used to instruct the DataContractSerializer how to serialize a particular member.

Order tells the DataContractSerializer what order to serialize/deserialize the member in.

EmitDefaultValue tells the DataContractSerialzier whether or not to emit the default values (null for reference types, a given value for value types) when serializing. If it is set to false, null won't be the result - the member will be ommitted. It is true by default.

A couple of examples might help to illustrate this:

First, the class. If you look at the class in the debugger, the properties are going to be listed in ASCII alphabetical order, which means step10 will be listed first. So will column10.

If you look at the serialized XML, you might see something like this:

<column8>abcd</column8>
<column9>defg</column9>
<column10>hijk</column10>

If one of the columns, say column9, was a null string, you'd see this (with EmitDefaultValue set to false) when it was serialized:

<column8>abcd</column8>
<column10>zzzz</column10>

EmitDefaultValue can be a little confusing from a naming perspective. And it has some really fun interactions with IsRequired.

Tim
  • 28,212
  • 8
  • 63
  • 76