How to get DataMemberAttribute to use boolean type

I have a simple test application that pulls an XML document from a rest interface. A data item has a couple of string fields and a couple of boolean fields. I create a simple entity class and put a DataContractAttribute on it and then add DataMemberAttributes to each data member. Then I use HttpResponseMessage.Content.ReadAsDataContract () to parse the response. All string types go through fine, but all my booleans are false (and they are not really false). The xml element looks something like this:

<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 correct boolean values?

+2


source to share


2 answers


Believe it or not, the DataContractSerializer is sensitive to the order of the elements in the descripted XML document. I'm sure you need to set the Order property of the DataMemberAttribute to match the actual "is-enabled" position among the other children of its parent.



+1


source


It seems like it should work, but ... the first thing I try is to remove the dash "-" from the data member name. It is possible that the serializer (de) loops on the dash inside and does not display the correct member and hence gives you a default bool value of false



If you feel the need to complicate the name of a compound variable, try underlining.

0


source







All Articles