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

标签 c# .net wcf serialization xml-namespaces

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

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

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

但是,Web 服务要求类和字段以命名空间为前缀,比如说命名空间 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>

最佳答案

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

使用什么前缀对于使用文档的人来说并不重要,只要它指向相同的确切 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 命名空间和信息集的概念)。

关于c# - 如何向 WCF 消息序列化中的字段添加 XML 前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3833854/

相关文章:

c# - .NET/C# - 反射帮助 - 程序集中的类

c# - 使用 FFMPEG 进程从 rtsp 获取所有帧

c# - 是什么导致 RoleEnvironment 初始值设定项中的 ModuleLoadException?

c# - 如何使用 WCF 从 Windows 服务通知应用程序

c# - 如何防止 CPU : Synchronous method calling multiple workers asynchronously & throttling using SemaphoreSlim? 的 "maxing out"

c# - 如何让我的 Windows 服务以特定的时间间隔运行?

javascript - 对我不明白的javascript的好处

c# - C# 是否有一种自然的方法来检查单个字符串和多个正则表达式的正则表达式匹配,以及单次传递字符串?

c# - 如何在.net框架上使用vst sdk

c# - C# 中的 HTTP 服务器支持分块传输编码