Nil Pun
Nil Pun

Reputation: 17373

How to deserialize element with attribute

I'm consuming a RESTful web service by using RESTSharp. One of the XML elements looks like the following:

<temp_c units="°C">7.9</temp_c>

And the C# class POCO is as follows:

public class Test
{
    public TempC temp_c { get; set; }
}

public class TempC
{
    public string units { get; set; }
    public string value { get; set; }
}

When I use RESTSharp, I get the TempC object populated with units but not with an actual value; e.g. 7.9. The value is NULL.

Upvotes: 4

Views: 1609

Answers (2)

Dhrumil Bhankhar
Dhrumil Bhankhar

Reputation: 1321

You need to put [XmlText] annotation in that case

public class TempC
    {
        public string units { get; set; }

        [XmlText]
        public string value { get; set; }
    }

This will tell De-serializer to get that from body of tag.

Reference link : https://groups.google.com/forum/#!topic/microsoft.public.dotnet.xml/loj2CBoyCnE

Upvotes: 0

Nil Pun
Nil Pun

Reputation: 17373

Fixed the problem by changing the property value to Value.

More detail example is here: https://github.com/restsharp/RestSharp/wiki/Deserialization

Upvotes: 3

Related Questions