I am having some troubles with the deserialization of my xml string to my object. I am not getting any errors but the values aren't populating (the values aren't null
they are just ""
). I've looked at a few questions that had the same issue but those problems usually consisted of people not having the [XmlRoot]
or [XmlElement]
defined.
Here is a bit of my xml string:
string xmlString = @"<results><dpv_answer value=""Y"" /><zip value=""95118-4007"" /></results>"
Here is the function to 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 is not XML, simply return defaultXmlClass
}
}
return address;
Here is a bit of the 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; }
}