【问题标题】:RestSharp - WP7 - Cannot deserialize XML to a listRestSharp - WP7 - 无法将 XML 反序列化为列表
【发布时间】:2012-04-19 10:57:02
【问题描述】:

我在我的 Windows Phone 7.1 项目中使用 RestSharp。

我在这里有一个 XML 格式的响应: https://skydrive.live.com/redir.aspx?cid=0b39f4fbbb0489dd&resid=B39F4FBBB0489DD!139&parid=B39F4FBBB0489DD!103&authkey=!AOdT-FiS6Mw8v5Y

我试图反序列化对该类的响应:

public class fullWall
{
    public _user user { get; set; }
    public int numberOfFriend { get; set; }
    public int numberOfPhoto { get; set; }
    public List<timhotPhotos> timhotPhotos { get; set; }
    public fullWall()
    {
        timhotPhotos = new List<timhotPhotos>();
    }
}

所有属性都可以,除了timhotPhotos 列表,您可以在此处看到:

timhotPhotos 类:

public class timhotPhotos
{
    public string id { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public string url { get; set; }
    public double width { get; set; }
    public double height { get; set; }
    public DateTime createdDate { get; set; }
    public _user user { get; set; }
    public int numOfComment { get; set; }
    public int numOfRate { get; set; }
    public int numOfView { get; set; }
    public bool rated { get; set; }
}

我哪里错了?

【问题讨论】:

  • 我会写几行代码将一些对象序列化为 XML,然后检查生成的 XML 文件和您的 XML 文件之间的差异
  • 尝试删除 fullWall 构造函数或删除 timhotPhotos 初始化

标签: windows-phone-7 c#-4.0 deserialization restsharp


【解决方案1】:

您必须将默认 XML 反序列化器更改为 DotNetXmlDeserializer,如下所示:

RestClient client;

client.AddHandler("application/xml", new DotNetXmlDeserializer());

然后,将XmlElement 属性添加到List&lt;timhotPhotos&gt; timhotPhotos 属性,如下所示:

public class fullWall
{
    public _user user { get; set; }
    public int numberOfFriend { get; set; }
    public int numberOfPhoto { get; set; }
    [System.Xml.Serialization.XmlElement()]
    public List<timhotPhotos> timhotPhotos { get; set; }
    public fullWall()
    {
        timhotPhotos = new List<timhotPhotos>();
    }
}

现在应该可以正常工作了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多