c# - datacontractserializer deserialize list<> always empty -


i'm deserializing data received web-service.

the problem deserialization of list returns empty list , no exception generated. can me figure out why? have tried several possible syntax. code below closest correct solution cannot deserialize correctly list of classes.

<arrayofbatch xmlns="http://schemas.datacontract.org/2004/07/myns" xmlns:i="http://www.w3.org/2001/xmlschema-instance">  <maasbatch> <batchname>all users</batchname> <users> <maasusers>     <firstname>bob</firstname>     <lastname>thetest</lastname>             <sourceemail>bob@source.com</sourceemail>     <sourcetenantid>111</sourcetenantid>     <targetemail>bob@target.com</targetemail>     <targettenantid>222</targettenantid> </maasusers> </users> </maasbatch> </arrayofbatch> 

code:

 list<maasbatch> lstmaasbatches = null;         try         {                             string target = string.empty;             using (var response = request.getresponse())             {                 stream streamreader = response.getresponsestream();                 datacontractserializer serializer = new datacontractserializer(typeof(list<maasbatch>));                 lstmaasbatches = (list<maasbatch>)serializer.readobject(streamreader);                 streamreader.close();                                }             return lstmaasbatches;         }         catch (exception exc)         {             return lstmaasbatches;         }     

class:

 [datacontract(name = "maasbatch", namespace = "http://schemas.datacontract.org/2004/07/myns")] [knowntype(typeof(maasusers))] public class maasbatch {     [datamember]     public string batchname { get; set; }     [datamember]     public list<maasusers> users { get; set; }      [ondeserializing]     internal void ondeserializingcallback(streamingcontext streamingcontext)     {         this.users = new list<maasusers>();     } }  [datacontract(name = "maasusers", namespace = "http://schemas.datacontract.org/2004/07/myns")]     public class maasusers {      [datamember]     public string firstname { get; set; }      [datamember]     public string lastname { get; set; }      [datamember]     public string sourceemail { get; set; }      [datamember]     public int sourceagentid { get; set; }      [datamember]     public string targetemail { get; set; }      [datamember]     public int targetagentid { get; set; }  } 

try add order , name attribute contract class. sample:

[datamember(order = 1, name = "firstname")] 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -