c# - XML serializer returns null on object deserialization -
i have stored procedure in database returns xml stream, , application deserializes stream corresponding object. stored procedure defined (i simplified make more readable):
select usrs.firstname 'firstname', usrs.lastname 'lastname', usrs.username 'username', usrs.datejoined 'datejoined' users usrs usrs.username = @username xml path('userprofile')
notice username
primary key, stored procedure return 1 result. sample query result this:
<userprofile> <firstname>chuck</firstname> <lastname>norris</lastname> <username>chuck.awesome</username> <datejoined>2013-07-22t06:58:00</datejoined> </userprofile>
now in application, how , deserialize data:
internal static t getdata<t>(storedprocedures storedprocedure, parameterlist parameters) { using (var connection = getsqlconnection()) { using (var command = new sqlcommand(storedprocedure.tostring(), connection)) { command.commandtype = system.data.commandtype.storedprocedure; foreach (var parameter in parameters) { command.parameters.add(new sqlparameter(parameter.key.tostring(), parameter.value)); } connection.open(); var data = command.executescalar(); return deserializexml<t>(data.tostring()); } } }
and deserializexml<t>()
method:
private static t deserializexml<t>(string xmlstream, type[] additionaltypes = null) { xmlserializer serializer; if (additionaltypes == null) { serializer = new xmlserializer(typeof(t)); } else { serializer = new xmlserializer(typeof(t), additionaltypes); } using (stringreader reader = new stringreader(xmlstream)) { return (t)serializer.deserialize(reader); } }
and userprofile
class:
[xmlroot("userprofile")] [serializable] public class userprofile { public userprofile() { } [xmlattribute("username")] public string username { get; set; } [xmlattribute("firstname")] public string firstname { get; set; } [xmlattribute("lastname")] public string lastname { get; set; } [xmlattribute("datejoined")] public datetime datejoined { get; set; } }
now when run application, see stored procedure returns expected value, however, serializer returns userprofile
object fields set null
(except datejoined
field, set default value since it's not nullable). idea going wrong? suspect might xmlroot()
attribute in userprofile
object, again serializer doesn't throw exception why i'm confused. idea might going wrong? in advance.
you've marked properties [xmlattribute]
xml contains values elements not attributes.
Comments
Post a Comment