无法使用 WCF web 服务的 XMLSerializer 结果反序列化 [英] Cannot deserialize with XMLSerializer result from WCF webservice

查看:23
本文介绍了无法使用 WCF web 服务的 XMLSerializer 结果反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从紧凑框架中尝试获取http服务的代码..

Here is the code trying from compact framework to get http service..

    List<Table> tables;
    using (Stream r = response.GetResponseStream())
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Table),"http://schemas.datacontract.org/2004/07/");
        tables=(List<Table>) serializer.Deserialize(r);
    }

   response.Close();

失败并显示 {"XML 文档 (1, 2) 中存在错误."}

It fails with {"There is an error in XML document (1, 2)."}

{"<ArrayOfTable xmlns='http://schemas.datacontract.org/2004/07/WpfApplication1.Data.Model'> was not expected."}

表命名空间相同...我不知道那里出了什么问题...

Table namespace is the same... I dont know whats wrong there...

更新

问题是我有 typeof(Table) 而不是 typeof(List

) 部分工作..没有错误,但创建的表值为空!

Problem was that i had typeof(Table) not typeof(List<Table>) which works partially.. No error but created tables values are null!

推荐答案

XmlSerializer 构造函数的第二个参数适用于序列化和反序列化.因此,第二个参数(命名空间)应该与接收到的参数相同.所以你最终会得到:

The second parameter on the XmlSerializer constructor works for both serializing and deserializing. So, on the second parameter (the namespace) should be the same to the one being received. So you'll end up having:

XmlSerializer serializer = new XmlSerializer(typeof(Table),"http://schemas.datacontract.org/2004/07/WpfApplication1.Data.Model")

注意命名空间字符串末尾的WpfApplication1.Data.Model".

Note the "WpfApplication1.Data.Model" at the end of the namespace string.

摆脱命名空间的一种方法.是在你的模型类(表)上指定它不应该使用命名空间:

One way to get rid of the namespace thing. Is to specify on your model class (Table) that it should not use a namespace:

[DataContract(Namespace = "")]
public class Table { ... }

这样你就不需要为反序列化指定命名空间.

That way you don't need to specify the namespace for deserialization.

希望有帮助!

这篇关于无法使用 WCF web 服务的 XMLSerializer 结果反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆