1

I'm trying to deserialize an XML stream into an object. It works perfectly for all but one field and I can't figure out what is wrong with that field. To make sure I know what I'm dealing with, I've coded the XML string directly.

[DataContract(Name = "auth")]
public class Authorization
{
    [DataMember(Name = "status")]
    public string Status { get; set; }
    [DataMember(Name = "user_name")]
    public string UserName { get; set; }
    [DataMember(Name = "person_pk")]
    public string PersonID { get; set; }
}

StringBuilder sb = new StringBuilder();
sb.Append("<auth xmlns=\"http://schemas.datacontract.org/2004/07/Veracross\">");
sb.Append("<status>success</status>");
sb.Append("<person_pk>2516</person_pk>");
sb.Append("<user_name>scohen</user_name>");
sb.Append("</auth>");

string fixedXMLData = sb.ToString();

DataContractSerializer serializer = new DataContractSerializer(typeof(Authorization));
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(fixedXMLData));
Authorization Auth = (Authorization)serializer.ReadObject(stream);

When I test the Auth object after execution I find that Status and UserName are properly populated. PersonID is null. I've tried changing the field names in the XML string, reordering the fields, changing the content of the person_pk field, etc. Nothing makes it work.

Now here's where it gets weird - If I change "person_pk" to "test" everywhere, it works fine and I get the correct value for PersonID. However, if I swap "person_pk" for "person" or "pk" it still doesn't work? In reality, I'm getting this XML string from a RESTful service and have no control over the naming of the fields. The field is named "person_pk", I just can't figure out why it won't properly deserialize.

Any idea?

Thanks

THE SOLUTION: I updated my DataMember attributes to include the Order attribute:

[DataContract(Name = "auth")]
public class Authorization
{
    [DataMember(Name = "status", Order=0)]
    public string Status { get; set; }
    [DataMember(Name = "person_pk", Order=1)]
    public string PersonID { get; set; }
    [DataMember(Name = "user_name", Order=2)]
    public string UserName { get; set; }
}
BKahuna
  • 601
  • 2
  • 11
  • 23

1 Answers1

0

Change the order of fields in XML file so that person_pk comes before status:

sb.Append("<auth xmlns=\"http://schemas.datacontract.org/2004/07/Veracross\">");
sb.Append("<person_pk>2516</person_pk>");
sb.Append("<status>success</status>");
sb.Append("<user_name>scohen</user_name>");
sb.Append("</auth>");

DataSerializer considers the fields order too.

UPDATE: You may be interested in this discussion: Ignore field order in DataContractSerializer

Community
  • 1
  • 1
ogggre
  • 2,204
  • 1
  • 23
  • 19
  • Actually I can't change the order of the fields in the XML string as I receive that from a service I don't control. However, the posting you pointed to pointed to an MSDN article that had the answser. I've updated my question to include the answer. Thanks – BKahuna Feb 25 '12 at 06:29