Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing XML doesn't work for one field
    primarykey
    data
    text
    <p>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.</p> <pre><code>[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("&lt;auth xmlns=\"http://schemas.datacontract.org/2004/07/Veracross\"&gt;"); sb.Append("&lt;status&gt;success&lt;/status&gt;"); sb.Append("&lt;person_pk&gt;2516&lt;/person_pk&gt;"); sb.Append("&lt;user_name&gt;scohen&lt;/user_name&gt;"); sb.Append("&lt;/auth&gt;"); string fixedXMLData = sb.ToString(); DataContractSerializer serializer = new DataContractSerializer(typeof(Authorization)); MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(fixedXMLData)); Authorization Auth = (Authorization)serializer.ReadObject(stream); </code></pre> <p>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.</p> <p>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.</p> <p>Any idea?</p> <p>Thanks</p> <p>THE SOLUTION: I updated my DataMember attributes to include the Order attribute:</p> <pre><code>[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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload