2010-09-30 74 views
3

如何添加XML前綴到WCF消息序列化中的字段?如何向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> 
+0

你可以編輯這個服務的WSDL來創建一個只包含失敗操作的WSDL,然後發佈編輯後的WSDL?瞭解這是RPC還是文檔服務等是非常重要的。 – 2010-09-30 19:00:23

+0

@John,請參閱更新的問題 – Brandon 2010-09-30 19:09:40

+0

您需要包含WSDL的其餘部分。這是將聲明這些名稱空間聲明的部分,等等。 – 2010-09-30 19:10:16

回答

-2

嘗試定義MyClass的是:

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

    [DataMember] 
    Field2 ... 
} 

UPDATE 這不會創建前綴,但如果XML具有正確的名稱空間並且Java應該可以工作,則不需要前綴。

+0

這將定義** XML名稱空間** - 但是** NOT **在XML有效載荷的序列化中使用的任何前綴.... – 2010-09-30 19:01:43

+0

請參閱我的更新... – Aliostad 2010-09-30 19:05:40

+1

將其更改爲但web服務只是抱怨xmlns是意外的內容.. – Brandon 2010-09-30 19:26:08

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模式不要求應該用什麼前綴。

Aliostad的DataContract示例正是如何定義Data Contract Serializer將使用的名稱空間。無法定義DataContract序列化程序將使用的前綴,因爲前綴無關緊要。只要使用這種XML的服務符合XML標準(並且不像RegEx表達式,並且相信我,我已經看到很多情況,XML的使用者是定製的書寫文本解析器而不是使用XML解析器並沒有掌握XML命名空間和Infosets的概念)。

+0

我傾向於不是真正的XML解析器,因爲xmlns =使它成爲barf ...: ( – Brandon 2010-10-04 02:12:23

+0

你是否曾經解決過你的問題?因爲我有類似的問題,並通過擴展IClientMessageFormatter並將它掛接到WCF調用來解決它。 – 2011-08-08 06:58:00