1

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; }
}
B-M
  • 1,231
  • 1
  • 19
  • 41
  • You have an empty `catch {}` handler, you might be getting errors but not knowing it? – Alfie Aug 23 '13 at 19:03
  • I'm stepping through using the debugger and I am not going into the catch block. – B-M Aug 23 '13 at 19:04
  • Off-topic comment: it's better to indent your code with spaces instead of tabs. – BartoszKP Aug 23 '13 at 19:08
  • How about [Serializable] in your class? Try this. –  Aug 23 '13 at 19:09
  • @AlexandreVicenzi I'll give that a try after I try out fcuesta's answer – B-M Aug 23 '13 at 19:12
  • @AlexandreVicenzi [Serializable] is not actually used for XML serialization (despite the name); see [here](http://stackoverflow.com/questions/2894431/required-attributes-in-xml-serialization) – paul Aug 23 '13 at 19:17

2 Answers2

4

dpv_answer and zip are complex elements not just a string. Try the 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; }
}
fcuesta
  • 4,429
  • 1
  • 18
  • 13
3

each of your elements have attributes that you are trying to get the values from. Also, the attributes are the same, so instead of having multiple serializable classes, as fcuesta suggested, you can use a single class, like so:

[XmlRoot("results")]
public class StandardAddress
{
    [XmlElement(ElementName = "dpv_answer")]
    public Element DpvAnswer { get; set; }

    [XmlElement(ElementName = "zip")]
    public Element Zip { get; set; }
}

public class Element
{
    [XmlAttribute("value")]
    public string Value { get; set; }
}

This will work for like elements, which can be useful. But if you intend to alter your elements to their own unique schema in the future, they will need to be separate, as suggested by fcuesta.

Luke Willis
  • 8,429
  • 4
  • 46
  • 79