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

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

7.9 

而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。

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

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

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

 public class TempC { public string units { get; set; } [XmlText] public string value { get; set; } } 

这将告诉De-serializer从标签体中获取它。

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