xml - Deserialize from WebServices C# -
here's issue : need list of resources web services, , deserialize object. doesn't work, despite facts code worked xml file. can't figure why doesn't work, , i'm stuck !
here's xml :
<resourcedataset xmlns="http://schemas.microsoft.com/office/project/server/webservices/resourcedataset/"> <resources> <res_uid>blabla</res_uid> <res_name>blabla</res_name> <res_code>blabla</res_code> <res_group>blabla</res_group> <res_cost_center>blabla</res_cost_center> </resources> <resources> <res_uid>blabla</res_uid> <res_name>blabla</res_name> <res_code>blabla</res_code> <res_group>blabla</res_group> <res_cost_center>blabla</res_cost_center> </resources> <resources> <res_uid>blabla</res_uid> <res_name>blabla</res_name> <res_code>blabla</res_code> <res_group>blabla</res_group> <res_cost_center>blabla</res_cost_center> </resources> </resourcedataset>
the class want deserialize :
using system; using system.collections.generic; using system.linq; using system.text; using system.xml; using system.xml.serialization; using system.threading.tasks; using system.collections; namespace testwpf { [serializable()] public class employee { [system.xml.serialization.xmlelement("res_uid")] public int res_uid { get; set; } [system.xml.serialization.xmlelement("res_name")] public string res_name { get; set; } [system.xml.serialization.xmlelement("res_code")] public string res_code { get; set; } [system.xml.serialization.xmlelement("res_group")] public string res_group { get; set; } [system.xml.serialization.xmlelement("res_cost_center")] public string res_cost_center { get; set; } public employee() { } public employee(int r_id, string res_name, string res_code, string res_group, string res_cost_center) { this.res_uid = r_id; this.res_name = res_name; this.res_code = res_code; this.res_group = res_group; this.res_cost_center = res_cost_center; } } [serializable()] [system.xml.serialization.xmlroot("resourcedataset")] public class employeelist //: ienumerator, ienumerable { public employeelist() {items = new list<employee>();} [xmlarray("resourcedataset")] [xmlarrayitem("resources")] public list<employee> items {get;set;} } }
and code use deserialize :
employeelist lstemployee = null; xmlserializer xs = new xmlserializer(typeof(serverslist)); streamreader sr = new streamreader("testemployee.xml"); lstemployee = (employeelist)serializer.deserialize(sr); reader.close(); (int = 0; < lstemployee.items.count(); i++) { messagebox.show(lstemployee.items[i].res_name); }
and when try launch receive error message :
firstly xml file invalid - res_uid expecting int, when serialization working you'll run problem.
you're not taking account namespace. following class works:
[serializable()] public class employee { [system.xml.serialization.xmlelement("res_uid")] public int res_uid { get; set; } [system.xml.serialization.xmlelement("res_name")] public string res_name { get; set; } [system.xml.serialization.xmlelement("res_code")] public string res_code { get; set; } [system.xml.serialization.xmlelement("res_group")] public string res_group { get; set; } [system.xml.serialization.xmlelement("res_cost_center")] public string res_cost_center { get; set; } public employee() { } public employee(int r_id, string res_name, string res_code, string res_group, string res_cost_center) { this.res_uid = r_id; this.res_name = res_name; this.res_code = res_code; this.res_group = res_group; this.res_cost_center = res_cost_center; } } [serializable()] [system.xml.serialization.xmlroot("resourcedataset", namespace = "http://schemas.microsoft.com/office/project/server/webservices/resourcedataset/")] public class employeelist //: ienumerator, ienumerable { public employeelist() {items = new list<employee>();} [xmlelement("resources", type = typeof(employee))] public list<employee> items {get;set;} } }
and calling code typos fixed:
employeelist lstemployee = null; xmlserializer xs = new xmlserializer(typeof(employeelist)); streamreader sr = new streamreader("testemployee.xml"); lstemployee = (employeelist)xs.deserialize(sr); sr.close(); (int = 0; < lstemployee.items.count(); i++) { messagebox.show(lstemployee.items[i].res_name); }
remember fix xml ints otherwise still won't work
Comments
Post a Comment