【问题标题】:How can I add a XML prefix to fields in WCF Message Serialization?如何向 WCF 消息序列化中的字段添加 XML 前缀?
【发布时间】:2010-09-30 18:54:08
【问题描述】:

如何向 WCF 消息序列化中的字段添加 XML 前缀?

我正在从 .NET 连接到 Java Spring Web 服务,并且我使用参数传入的对象正在按您的预期进行序列化:

<MyClass>
  <field1>Field 1 Value</field1>
  <field2>Field 2 Value</field2>
</MyClass>

但是,网络服务要求类和字段以命名空间为前缀,比如说命名空间blah,所以我想要的是:

<blah:MyClass>
  <blah:field1>Field 1 Value</blah:field1>
  <blah:field2>Field 2 Value</blah:field2>
</blah:MyClass>

如何在 WCF 中实现这一点?有没有办法调整我班级的 XML 序列化属性?

编辑:此特定实体的 WSDL 如下(已编辑以删除业务特定的字段名称,但其他一切都相同):

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch0="http://www.domain.com/app/schemas/entityone" xmlns:sch1="http://www.domain.com/app/schemas/types" xmlns:sch2="http://www.domain.com/app/schemas/query" xmlns:sch3="http://www.domain.com/app/schemas/entitytwo" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.domain.com/app/schemas/entityone" targetNamespace="http://www.domain.com/app/schemas/entityone">
  <wsdl:types>
  <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:types="http://www.domain.com/app/schemas/types" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://www.domain.com/app/schemas/entityone" xmlns:tns="http://www.domain.com/app/schemas/entityone">
  <import namespace="http://www.domain.com/app/schemas/types" /> 

<element name="TheClassName">
 <complexType>
  <sequence>
   <element name="field1" type="string" /> 
   <element name="field2" type="string" /> 
   <element name="field3" type="string" /> 
   <element name="field4" type="string" /> 
   <element name="field5" type="string" /> 
   <element name="field6" type="string" /> 
   <element name="field7" type="string" /> 
   <element name="field8" type="string" /> 
  </sequence>
 </complexType>
</element>

<wsdl:binding name="NameOfBindingHere" type="tns:ReturnTypeHere">
  <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
   <wsdl:operation name="OperationNameHere">
     <soap:operation soapAction="" /> 
     <wsdl:output name="ResponseTypeHere">
        <soap:body use="literal" /> 
     </wsdl:output>
   </wsdl:operation>
</wsdl:binding>

【问题讨论】:

  • 您能否编辑该服务的 WSDL 以创建一个仅包含失败操作的 WSDL,然后发布编辑后的 ​​WSDL?了解这是 RPC 还是 Document 服务等对我们来说很重要。
  • 您需要包含 WSDL 的其余部分。这就是将说明这些在哪个命名空间中声明的部分,等等。
  • @John,添加了我认为您所指的内容-混淆了一些实际名称,但应该代表那里的内容...
  • 您仍然遗漏了message 元素和portType。另外,您是如何使用这项服务的? “添加服务参考”?

标签: c# .net wcf serialization xml-namespaces


【解决方案1】:

我认为您混淆了名称空间和前缀。在您的示例中,Blah 是前缀,并且是指向命名空间的指针(别名)。在文档中,你会看到一个属性xmlns:blah="http://tempuri/your/namespace",前缀是blah,命名空间是http://tempuri/your/namespace

使用什么前缀对使用文档的人来说并不重要,只要它指向同一个确切的命名空间。

所以

<blah:MyClass xmlns:blah="http://tempuri/your/namespace"></blah:MyClass>

完全一样
<blah1:MyClass xmlns:blah1="http://tempuri/your/namespace"></blah1:MyClass>

XML Schema 不要求使用什么前缀。

Aliostad 的 DataContract 示例正是如何定义 Data Contract Serializer 将使用的命名空间。无法定义 DataContract Serializer 将使用什么前缀,因为无论前缀是什么都无关紧要。只要使用此 XML 的服务遵守 XML 标准(并且不像 RegEx 表达式,相信我,我已经看到很多案例,其中 XML 的使用者是自定义编写的文本解析器而不是使用 XML 解析器,没有掌握 XML 命名空间和信息集的概念)。

【讨论】:

  • 我倾向于 not-a-real-XML-parser 因为 xmlns= 让它变得很糟糕...... :(
  • 您的问题解决了吗?因为我有一个类似的问题,并且通过扩展 IClientMessageFormatter 并将其连接到 WCF 调用实际上解决了它......
【解决方案2】:

尝试将 MyClass 定义为:

DataContract(namespace="blah")
Class MyClass
{
   [DataMember]
   Field1 ...

   [DataMember]
   Field2 ...
}

更新 这不会创建前缀,但如果 XML 具有正确的命名空间并且 Java 应该可以工作,则不需要前缀。

【讨论】:

  • 这将定义 XML 命名空间 - 但 NOT 在 XML 有效负载的序列化中使用的任何前缀......
  • 这会将其更改为 但网络服务只是抱怨 xmlns 是意外内容..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2019-11-11
  • 2020-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多