【问题标题】:ReadAsDataContract exception while reading namespace读取命名空间时出现 ReadAsDataContract 异常
【发布时间】:2011-02-10 10:24:07
【问题描述】:

我正在尝试使用 link 中提到的 WCF REST 入门工具包来使用 link 中提到的 twitter 的 REST api。

我在 DataContract 中使用与文章中提到的相同的对象 - statusList 和 status。

[assembly: ContractNamespace("", ClrNamespace = "TwitterShell")]
[CollectionDataContract(Name = "statuses", ItemName = "status")]
public class statusList : List<status> { }
public class user
{
    public string id;
    public string name;
    public string screen_name;
}
public class status
{
    public string id;
    public string text;
    public user user;
}

我正在使用 ReadAsDataContract() 方法读取 XML 内容。

HttpClient http = new HttpClient("http://twitter.com/statuses/");
http.TransportSettings.Credentials =
    new NetworkCredential("{username}", "{password}");
HttpResponseMessage resp = http.Get("friends_timeline.xml");
resp.EnsureStatusIsSuccessful();
statusList sList = resp.Content.ReadAsDataContract<statusList>();

我得到以下异常。我根本没有定义以下命名空间。

第 1 行位置 24 处出错。需要命名空间“http://schemas.datacontract.org/2004/07/sitename”中的元素“状态”。遇到名称为“状态”的“元素”,命名空间“”。

请帮忙。谢谢。

【问题讨论】:

    标签: wcf rest namespaces


    【解决方案1】:

    只是不要这样做。如果您尝试使用 Datacontracts 和操作合同访问非 wcf 服务,您将陷入痛苦的世界。

    好的,所以我想这有点不公平,让你别无选择,所以试试这个:

    var response = client.Get("http://twitter.com/statuses/friends_timeline.xml");
    
    var statuses = response.Content.ReadAsXElement();
    
    var statusQuery = from st in statuses.Elements("status")
                      select new status {
                                    id = st.Element("id").Value,
                                    text = st.Element("text").Value,
                                    user = (from us in st.Elements("user")
                                            select new user {
                                                 id = us.Element("id").Value,
                                                 name = us.Element("name").Value,
                                                 screen_name = us.Element("screen_name").Value
                                                             }).FirstOrDefault()
                                          };
    var statuses = statusQuery.ToList();
    

    使用 Linq to XML 从 XML 文档创建对象允许您避免序列化程序的魔力并完全控制客户端对象的名称和数据类型。将它包装为一个新的 HttpContent 扩展方法真的很容易,这样你就可以简单地做:

    var statuses = response.Content.ReadAsTwitterStatuses();
    

    【讨论】:

      【解决方案2】:

      我在寻找相同问题的答案时遇到了您的帖子,如果您想放弃 LINQ to XML 方法,我能够找到您正在寻找的解决方案。

      1) 确保使用以下装饰注释您的 Status 类

      [DataContract (Namespace = "")]
      

      通过指定上述注释,您将覆盖类的默认命名空间中的命名空间。这应该可以解决您的命名空间问题。

      2) 为了解决空值问题(我也遇到过),字段的顺序非常重要。当你的对象被反序列化时,它是按字母顺序完成的。您可以使用 DataMember 注释上的 Order 属性对字段进行排序以匹配传入 XML 的顺序。

      例如

      [DataMember (Order = 1)]
      public string text
      

      等等……

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-28
        相关资源
        最近更新 更多