.net - I need to know how to deserialize a specific XML into objects defined in a custom class in C# -
given following xml:
<?xml version="1.0" encoding="utf-8"?> <userattributelist> <attribute> <userid>12345678</userid> <attid>1234</attid> <attname>group</attname> <atttypeid>8</atttypeid> <atttypename>user group</atttypename> <attdata>member</attdata> </attribute> <attribute> <userid>12345678</userid> <attid>1235</attid> <attname>contact name</attname> <atttypeid>16</atttypeid> <atttypename>contact center greeting</atttypename> <attdata>john smith</attdata> </attribute> ... </userattributelist>
i want deserialize following classes:
[serializable] [xmltypeattribute(anonymoustype = true)] public class userattributelist { [xmlarray(elementname = "userattributelist")] [xmlarrayitem(elementname = "attribute")] public list<userattribute> attributes { get; set; } public userattributelist() { attributes = new list<userattribute>(); } } [serializable] public class userattribute { public string userid { get; set; } public string attid { get; set; } public string attname { get; set; } public string atttypeid { get; set; } public string atttypename { get; set; } public string attdata { get; set; } }
using code below, getresponsestream() returns xml object listed above:
xmlrootattribute xroot = new xmlrootattribute(); xroot.elementname = "userattributelist"; xroot.isnullable = true; xmlserializer serializer = new xmlserializer(typeof(userattributelist), xroot); try { return (userattributelist)serializer.deserialize(request.getresponse().getresponsestream()); } catch (exception exc) { return null; }
my code compiles no errors, userattributelist returned shows no child "attribute" items. no errors thrown
i sooner like:
public class userattributelist { [xmlelement] public list<userattribute> attribute { get; set; } public userattributelist() { attribute = new list<userattribute>(); } } public class userattribute { public int userid { get; set; } public int attid { get; set; } public string attname { get; set; } public int atttypeid { get; set; } public string atttypename { get; set; } public string attdata { get; set; } }
Comments
Post a Comment