views:

42

answers:

2

How can I add a XML prefix to fields in WCF Message Serialization?

I'm connecting up to a Java Spring web service from .NET, and an object that I pass in with parameters is being serialized as you would expect:

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

However, the web service requires that the class and fields are prefixed with a namespace, let's say namespace blah, so what I want is:

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

How can I make this happen in WCF? Is there a way to adjust the XML serialization attributes on my class?

Edit: WSDL for this particular entity is as follows (edited to remove business-specific field names, but everything else is the same):

<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"&gt;
  <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"&gt;
  <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>
A: 

Try defining MyClass as:

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

   [DataMember]
   Field2 ...
}

UPDATE This will not create the prefix but the prefix would not be needed if XML has the correct namespace and Java should work.

Aliostad
That will define the **XML namespace** - but **NOT** any prefix being used in serialization of the XML payload....
marc_s
See my update...
Aliostad
This changes it to <MyClass xmlns="blah"> and <field1 xmlns="blah"> but web service just complains about the xmlns being unexpected content..
routeNpingme
+1  A: 

I think you are confusing namespaces and prefixes. In your example Blah is the prefix and is a pointer (alias) to the namespace. In the document, you will see an attribute xmlns:blah="http://tempuri/your/namespace", the prefix is blah and the namespace is http://tempuri/your/namespace.

What the prefix is used does not matter to whomever consumes the document, as long as it points to the same exact namespace.

so

<blah:MyClass xmlns:blah="http://tempuri/your/namespace"&gt;&lt;/blah:MyClass&gt;

is the exact same thing as

<blah1:MyClass xmlns:blah1="http://tempuri/your/namespace"&gt;&lt;/blah1:MyClass&gt;

XML Schema does not require that what prefix should be used.

Aliostad's DataContract example is exactly how to define the namespace that the Data Contract Serializer will use. There is no way to define what prefix the DataContract Serializer will use, because whatever the prefix is doesn't matter. That is as long as the service consuming this XML is adheres to XML standards (and isn't something like a RegEx expression, and believe me I've seen plenty of cases where the consumer of the XML was a custom written text parser instead of using an XML parser and didn't grasp the concept of XML Namespaces and Infosets).

Don Demsak
I'm leaning toward not-a-real-XML-parser as the xmlns= makes it barf... :(
routeNpingme