How to deserialize element with attribute

◇◆丶佛笑我妖孽 提交于 2019-12-12 11:34:07

问题


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.


回答1:


Fixed the problem by changing the property value to Value.

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




回答2:


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



来源:https://stackoverflow.com/questions/12223369/how-to-deserialize-element-with-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!