Xml Deserialization with complex elements in c# -
this question has answer here:
- how deserialize xml document 12 answers
i having troubles deserialization of xml string object. not getting errors values aren't populating (the values aren't null
""
). i've looked @ few questions had same issue problems consisted of people not having [xmlroot]
or [xmlelement]
defined.
here bit of xml string:
string xmlstring = @"<results><dpv_answer value=""y"" /><zip value=""95118-4007"" /></results>"
here function deseralize:
standardaddress address = new standardaddress(); using (xmlreader reader = xmlreader.create(new stringreader(xml))) { try { address = (standardaddress)new xmlserializer(typeof(standardaddress)).deserialize(reader); } catch (invalidoperationexception x) { // string passed not xml, return defaultxmlclass } } return address;
here bit of object declaration:
[xmlroot("results")] public class standardaddress { [xmlelement(elementname = "dpv_answer")] public string dpv_answer { get; set; } [xmlelement(elementname = "zip")] public string zip { get; set; } }
dpv_answer
, zip
complex elements not string. try following:
[xmlroot("results")] public class standardaddress { [xmlelement(elementname = "dpv_answer")] public dpv_answer dpv_answer { get; set; } [xmlelement(elementname = "zip")] public zip zip { get; set; } } public class dpv_answer { [xmlattribute("value")] public string value { get; set; } } public class zip { [xmlattribute("value")] public string value { get; set; } }
Comments
Post a Comment