【问题标题】:How to deserialize element with attribute如何反序列化具有属性的元素
【发布时间】:2013-07-27 21:47:48
【问题描述】:

我正在使用 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。

【问题讨论】:

    标签: c# restsharp


    【解决方案1】:

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

    更详细的例子在这里: https://github.com/restsharp/RestSharp/wiki/Deserialization

    【讨论】:

      【解决方案2】:

      你需要在这种情况下放置 [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

      【讨论】:

        猜你喜欢
        • 2012-12-24
        • 2012-05-09
        • 1970-01-01
        • 2017-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-23
        • 2015-09-24
        相关资源
        最近更新 更多