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; }
}
6
  • You have an empty catch {} handler, you might be getting errors but not knowing it? Commented Aug 23, 2013 at 19:03
  • I'm stepping through using the debugger and I am not going into the catch block. Commented Aug 23, 2013 at 19:04
  • Off-topic comment: it's better to indent your code with spaces instead of tabs. Commented Aug 23, 2013 at 19:08
  • How about [Serializable] in your class? Try this. Commented Aug 23, 2013 at 19:09
  • @AlexandreVicenzi I'll give that a try after I try out fcuesta's answer Commented Aug 23, 2013 at 19:12

2 Answers 2

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

Yeah, I'll probably go with making the class for Element as you suggest. Cuts down on a lot of code then. Thanks.
@Brett Read my comment before doing it.
@YairNevet It worked fine for me, matched all the values to correct place.
@YairNevet The Element class has no concept of which element it is. The StandardAddress class makes that distinction.
@LukeWillis Yeah, you're right.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.