SPI
SPI

Reputation: 93

WCF SOAP Service add datatypes in response to complex types

I have a WCF SOAP Service in C# that runs perfect. But now I want to add the Datatypes to the SOAP Response.

If I return an array of boolean or string I get something like:

<a:elements xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
      <b:string>ID</b:string>
      <b:string>Description</b:string>
      <b:string>Material</b:string>
      <b:string>Price</b:string>
  </a:elements>

or

<a:updatePermissions xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
      <b:boolean>true</b:boolean>
      <b:boolean>false</b:boolean>
      <b:boolean>false</b:boolean>
      <b:boolean>false</b:boolean>
    </a:updatePermissions >

But if I send a complex type i get something like

<a:Elements>
      <a:Material>
        <a:Id>465</a:Id>
        <a:Description>TestAddMaterial</a:Description>
        <a:MaterialId>1</a:MaterialId>
        <a:PriceForKg>0.03</a:PriceForKg>
      </a:Material>
</a:Elements>

How can I convert the response of complex types like the response of an array?

Something like:

    <a:Elements>
      <a:Material>
        <a:integer>465</a:integer>
        <a:string>TestAddMaterial</a:string>
        <a:integer>1</a:integer>
        <a:double>0.03</a:double>
     </a:Material>
    </a:Elements>

The name of the Variable is not necessary at this point.

The class itselfe is a DataContract with DataMember.

Upvotes: 1

Views: 527

Answers (1)

William Xifaras
William Xifaras

Reputation: 5312

Technically you can return XElement. Any custom XML option would require you to build the XML yourself.

As an example:

[ServiceContract]
public interface ISomeService
{
   [OperationContract, XmlSerializerFormat]
   XmlElement SomeMethod(XmlElement someParameter);
}

You may also use the DocumentElement property of an XmlDocument instance as the default DataContractSerializer can serialize XmlElement instances.

https://msdn.microsoft.com/en-us/library/ms733901.aspx

Though the above options are not standard practice. Instead you should be using a DataContract and it should be structured in the way you want it to be serialized into XML. When the service method returns the custom type, WCF will serialize it into an XML document implicitly.

Upvotes: 1

Related Questions