views:

141

answers:

2

I have a simple test app that pulls an xml doc from a rest interface. The data element has a couple of string fields and a couple of boolean fields. I creates a simple entity class and put a DataContractAttribute on it and then added DataMemberAttributes to each data member. I then use HttpResponseMessage.Content.ReadAsDataContract() to parse the response. All the string types come through just fine but all of my boolean types are false (and they are not really false). The xml element is something like:

<is-enabled type="boolean">true</is-enabled>

and then in my type class I have something like:

[DataMember(Name="is-enabled")]
public bool isEnabled
{
    get
    {
        return this.isEnabledField;
    }
    set
    {
        this.isEnabledField = value;
    }
    }

How do I get the boolean values to come through properly?

A: 

It seems like it should work but... the first thing i would try is to remove the dash "-" from data member name. It's possible that the (de)serializer is choking on the dash internally and not mapping to the correct member and therefore giving you the default bool value of false;

If you feel the need to make the compound variable name more readable afterwards, try an underscore.

Paul Sasik
I can't change it, that is the name of the member from the service that I am consuming.
MikeD
+1  A: 

Believe it or not, DataContractSerializer is sensitive to the order of the elements in the XML document being deserialized. I bet you need to set the Order property of the DataMemberAttribute to match actual position of "is-enabled" amongst the other children of its parent element.

zvolkov