反序列化对命名空间和别名非常挑剔

反序列化对命名空间和别名非常挑剔

本文介绍了WCF XML 反序列化对命名空间和别名非常挑剔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 .net WCF SOAP 服务,试图与 Java 客户端集成.这个过程有点不寻常,因为客户端已经指定了WSDL,我必须创建一个可以接受他们请求的服务.

I am writing a .net WCF SOAP service, trying to integrate with a Java client. The process is a bit unusual, because the client has specified the WSDL, and I have to create a service which can accept requests from them.

当我们尝试执行集成时,我得到了一些非常挑剔的结果.非常细微的差异(在我看来似乎没问题)导致 XML 反序列化失败.

Im getting some very finicky results when we try to perform the integration. Very subtle differences (that appear to be ok to my eyes) cause the XML deserialization to fail.

这是客户端实际生成的 XML,它最终为我提供了 CCHNameSearchResponse 的所有子属性的空值,例如,在 XML 中设置为999"的 InterfaceControlField 遇到了空)

This is the XML actually produced by the client, which ends up giving me null values for all the child properties of CCHNameSearchResponse for example, the InterfaceControlField which is set to "999" in the XML comes across empty)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:soapclient.cch.doj.state.wi.us">
   <soapenv:Header/>
   <soapenv:Body>
      <impl:CCHNameSearchResponse xmlns:impl="urn:soapclient.cch.doj.state.wi.us" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <InterfaceControlField>999</InterfaceControlField>

这个有效,唯一的区别是命名空间别名 (xmlns:impl=) 在我看来它是可以接受的?

This one works, the only difference is the namespace alias (xmlns:impl=) which to my eyes looks like it acceptable?

xmlns:urn="urn:soapclient.cch.doj.state.wi.us">
   <soapenv:Header/>
   <soapenv:Body>
      <CCHNameSearchResponse xmlns="urn:soapclient.cch.doj.state.wi.us" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <InterfaceControlField xmlns="">999</InterfaceControlField>

为什么?我怎样才能让它接受生成的 XML?

推荐答案

问题是如果没有别名,命名空间将成为所有子元素的默认命名空间.

The issue is that without an alias, the namespace becomes the default namespace for all child elements.

使用别名,子元素位于无命名空间"中,因此在序列化中没有被正确选取.

With the alias, the child elements are in the "no-namespace", and therefore are not picked up correctly in the serialization.

这是每个规范,因此为我生成 XML 的方面存在问题.

This is per spec, so its a problem with the side generating the XML for me.

http://www.w3.org/TR/xml-names/#违约

因此,要么不使用别名(根据 OP 中的第二个示例),要么必须在任何地方使用别名,如此处的示例所示

So either no alias should be used (per the second example in the OP) or the alias must be used everywhere, as in the sample here

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:soapclient.cch.doj.state.wi.us">
   <soapenv:Header/>
   <soapenv:Body>
      <impl:CCHNameSearchResponse xmlns:impl="urn:soapclient.cch.doj.state.wi.us" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <impl:InterfaceControlField>999</impl:InterfaceControlField>

这篇关于WCF XML 反序列化对命名空间和别名非常挑剔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 18:24