加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XML序列化 – 客户端缺少命名空间前缀

发布时间:2020-12-16 23:22:28 所属栏目:百科 来源:网络整理
导读:我创建了一个.NET Web服务,它返回一个对象,比如Class“getResponse”. WS返回以下响应…… getResponse xmlns="http://tempuri.org/getCustomer/wsdl/" Result xmlns="http://tempuri.org/getCustomer/" ResultCodeOK/ResultCode ErrorsList/ /Result /getRe
我创建了一个.NET Web服务,它返回一个对象,比如Class“getResponse”.

WS返回以下响应……

<getResponse xmlns="http://tempuri.org/getCustomer/wsdl/">
   <Result xmlns="http://tempuri.org/getCustomer/">
      <ResultCode>OK</ResultCode>
      <ErrorsList/>
   </Result>  
</getResponse>

而客户端实际上正在等待以下…(注意“mes-root:”前缀)

<mes-root:getResponse xsi:schemaLocation="http://tempuri.org/getCustomer/ getCustomer_V200906.xsd" xmlns:mes-root="http://tempuri.org/getCustomer/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <mes-root:Result>
  <mes-root:ResultCode/>
  <mes-root:ErrorsList/>
 </mes-root:Result>
</mes-root:getResponse>

我怎么能做到这一点?我是否需要在getResponse类上设置某些XML Serialization atttibutes,以便在客户端显示mes-root前缀?

编辑:我在下面的位置http://forums.asp.net/t/1249049.aspx发现了一个类似的问题.说实话,我不太了解它,我无法让它工作.

解决方法

在通常情况下,客户端必须符合Web服务发送的响应类型.但是,您的情况似乎有所不同,因为您似乎正在构建一个Web服务,为预先存在的客户端提供格式化的响应.

要解决名称空间前缀问题,您在问题中提到的链接提供了适当的解决方案;您需要在序列化过程中“引导”XmlSerializer,并且可以通过将XmlNamespaceDeclarations属性指定给返回XmlSerializerNamespaces类型的对象的属性来执行此操作.该属性也需要设置,否则将不会应用命名空间.

将以下代码添加到getResponse类时,xml响应将按照示例中的预期格式进行:

[XmlNamespaceDeclarations()] 
public XmlSerializerNamespaces xmlsn 
{ 
  get 
  { 
    XmlSerializerNamespaces xsn = new XmlSerializerNamespaces(); 
    xsn.Add("mes-root","http://tempuri.org/getCustomer/"); 
    return xsn; 
  } 
  set 
  {
  //Just provide an empty setter. 
  } 
}

为这样的Web服务生成的WSDL类的分析揭示了以下方法(“GetMyResponse”是我给WSs方法返回GetResponse对象的名称):

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/getCustomer/GetMyResponse",RequestNamespace = "http://tempuri.org/getCustomer/",ResponseNamespace = "http://tempuri.org/getCustomer/",Use = System.Web.Services.Description.SoapBindingUse.Literal,ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] 
public GetResponse GetMyResponse() 
{ 
  object[] results = this.Invoke("GetMyResponse",new object[-1 + 1]); 
  return (GetResponse)results(0); 
}

我相信RequestNamespace和ResponseNamespace属性有所不同.

希望这能解决一些问题,理解这里发生的基础Xml序列化.

编辑(评论后)

以下是我通过Test Webservice收到的回复:

<?xml version="1.0" encoding="utf-8"?>
<mes-root:GetResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:mes-root="http://tempuri.org/getCustomer/">
  <mes-root:Result>OK</mes-root:Result>
  <mes-root:ErrorsList>
    <mes-root:string>SomeErrors</mes-root:string>
    <mes-root:string>SomeMoreErrors</mes-root:string>
  </mes-root:ErrorsList>
</mes-root:GetResponse>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读