无法使用根名称“Binary"和根命名空间反序列化 XML 正文? [英] Unable to deserialize XML body with root name 'Binary' and root namespace?

查看:37
本文介绍了无法使用根名称“Binary"和根命名空间反序列化 XML 正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我(客户端)试图通过 ASP.NET WCF 与网络服务进行通信,并且已经解决了这个问题(更多信息在这里:使用 WCF 通过 HTTP 连接到受密码保护的服务器).

I am(client) trying to communicate with a webservice with ASP.NET WCF and have come along way to solve this(more information here : Connect to a password protected server with WCF over HTTP).

运行请求方法时:

var proxy = factory.CreateChannel();
var response = proxy.GetMyData(id);

我收到以下异常:

无法使用根名称Binary"和根命名空间"反序列化 XML 主体(对于操作GetMyData"和合同(ICFService",http://tempuri.org/')) 使用 XmlSerializer.确保将 XML 对应的类型添加到服务的已知类型集合中.说明:在执行当前 Web 请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其起源于代码的更多信息.异常详细信息:System.Runtime.Serialization.SerializationException:无法反序列化具有根名称Binary"和根命名空间"的 XML 主体(对于操作GetMyData"和合同(ICFService",http://tempuri.org/')) 使用 XmlSerializer.确保将 XML 对应的类型添加到服务的已知类型集合中.

Unable to deserialize XML body with root name 'Binary' and root namespace '' (for operation 'GetMyData' and contract ('ICFService', 'http://tempuri.org/')) using XmlSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.Serialization.SerializationException: Unable to deserialize XML body with root name 'Binary' and root namespace '' (for operation 'GetMyData' and contract ('ICFService', 'http://tempuri.org/')) using XmlSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.

这可能是编码问题吗?我该如何解决?

Could this be a encoding problem? How do I solve it?

此致

Edit1 : 我在服务"的界面中添加了以下行:

Edit1 : I have added the following line to the interface of the "service" :

[ServiceKnownType(typeof(MyObject))]

但这并不能解决问题.

我还想补充一点,现在抛出了一个新的异常:

I also want to add that a new exception have bin thrown now :

根级别的数据无效.第 1 行,位置 1.说明:在执行当前 Web 请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其起源于代码的更多信息.异常详细信息: System.Xml.XmlException: 根级别的数据无效.第 1 行,位置 1.

这通常是我得到的第一个异常,但是当再次尝试时我得到另一个异常?

This is often the first exception I get but when trying again I get the other exception?

Web 服务设置为返回两种类型的 XML 文档,真实消息包含一些信息,为了在我的应用程序中对其进行反序列化,我创建了具有 XmlRoot 和 XmlElement 属性的对应类(这是我添加为的类)服务接口的已知类型).可以返回的另一条消息将只包含一个字符串,当发生这种情况时,我的客户端可能会抛出异常,因为它试图将数据反序列化为不支持它的类型.当我得到正确的响应时抛出第一个异常(无法反序列化具有根名称二进制"的 XML 主体),而第二个异常(根级别的数据无效.第 1 行,位置 1)可能是在我收到不支持的消息.

The webservice is set up to return two types of XML documents, the real message contains some information and to deserilize this in my applicaiton I have created the corsponding classes with XmlRoot and XmlElement attributes (this is the class that I have added as known typ to the service interface). The other message that can be returned will only contain a string and when this happen an exception will probably be thrown in my client becouse It is trying to deserlize data into a type that do not support it. Its possible that the first exception (Unable to deserialize XML body with root name 'Binary') is thrown when I get a propper response and the second exception ( The data at the root level is invalid. Line 1, position 1 ) is probably when I get the not supported message.

我认为属性和 channelfactory 应该使它变得容易,但我看不到我返回什么 XML,所以我现在考虑将我的解决方案切换到使用 XMLDocumet 类的简单工作.这不是一个好的解决方案,但我会确切地看到发生了什么.

I thought that the attributes and the channelfactory should make it easy but I can´t see what XML I get back so Im now thinking of switching my solution to simple work with the XMLDocumet class. This is not a good solution but I will see exacly what´s happening.

Edit2

如果我在自己的计算机上针对 webservice 运行 URL,我将得到以下回复:

If I run the URL against the webservice on my own computer I will get the following reply :

<Error><code>E50</code><desc>IP Not Allowed</desc></Error>

仅此而已,如果我在正确的服务器(我的主机)上运行它,重播应该根据客户是这样的:

Nothing more, and If I run it on the correct server(my host) the replay should according to the customer be something like this :

<Desc>
<Make cfe_code="98" cfe_value="Volkswagen" label="Märke" value="Volkswagen"/>
<ModelName cfe_code="99" cfe_value="Touareg" label="Modell" value="Touareg"/>
<BodyType cfe_code="212" cfe_value="22" label="Kaross" value="SUV"/>
...
</Desc>

编辑 3:

我现在已经在主机服务器上尝试了这个代码:

I have now tried this code on the host server :

public XDocument GetMyData(string regNr)
{
  WebClient wc = new WebClient();
  wc.Credentials = new NetworkCredential("MyName", "MyPassword");

  Stream s = wc.OpenRead("http://MyServer.com/ag/get?UID=9999999999.eu_vddsall_xml&VINREG=" + regNr + "&LANG=sv");

  // return an XDocument for LINQ
  XmlTextReader xmlReader = new XmlTextReader(s);
  XDocument xdoc = XDocument.Load(xmlReader);
  xmlReader.Close();

  xdoc.Save(HostingEnvironment.MapPath("~/App_Data/Files/Xml/") + DateTime.Now.ToString("hhmmssfff") + ".xml");

  return xdoc;
}

这很好用!我得到一个包含以下内容的 XML 文件:

And this works great! I get a XML file that contains the following :

<?xml version="1.0" encoding="utf-8"?>

<Desc>
 <Vin cfe_code="11" cfe_value="X" label="Chassie nr" value="X" />
 <Make cfe_code="98" cfe_value="Volvo" label="Märke" value="Volvo" />
 <ModelName cfe_code="99" cfe_value="S70" label="Modell" value="S70" />
 <BodyType cfe_code="212" cfe_value="1" label="Kaross" value="Sedan" />
...
</Desc>

那么为什么我的 WCF 版本不起作用?

So why does not my WCF version work?

推荐答案

这是我学到的:

WCF 能够构建 REST 服务,但 WCF 不为使用此服务提供额外帮助.相反,您应该使用 WebClient 或 HttpWebRequest 与 REST 服务进行通信.然而,WCF 确实提供了一种与 SOAP 服务通信的方式,否则这将很难做到.

WCF is able to build REST services but WCF does not provide additional help for consumption of this service. Instead you should use WebClient or HttpWebRequest to communicate with the REST service. WCF does however provide a way of communicating with SOAP services else this would be vary hard to do.

所以我帖子的 Edit3 是与 REST 服务通信的正确方式.

So the Edit3 of my post is the correct way of communicating with a REST service.

在我的例子中,xDocument 会将来自 WebClient 的输出解析为 XDocument.然后我将不得不手动将此 XDocument 转换为我的视图类(ASP.NET MVC)

In my case the xDocument will pars the output from the WebClient to a XDocument. Then I will manually have to convert this XDocument to my view classes (ASP.NET MVC)

这篇关于无法使用根名称“Binary"和根命名空间反序列化 XML 正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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