如何使用属性反序列化元素

时间:2012-08-31 22:52:16

标签: c# restsharp

我正在使用RESTSharp来使用RESTful Web服务。其中一个XML元素如下所示:

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

C#类POCO如下:

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

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

当我使用RESTSharp时,我得到的TempC对象填充了单位但没有实际值;例如7.9。值为NULL。

2 个答案:

答案 0 :(得分:3)

通过将属性值更改为Value来解决此问题。

更多细节示例如下: https://github.com/restsharp/RestSharp/wiki/Deserialization

答案 1 :(得分:0)

你需要在这种情况下放置[XmlText]注释

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

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

这将告诉反序列化器从标记体中获取它。

参考链接:https://groups.google.com/forum/#!topic/microsoft.public.dotnet.xml/loj2CBoyCnE

相关问题