Condividi tramite


Serialization magic with DataContract attribute in WCF

I want to serialize something similar to this format:

<Body>

      <Response>

                     <something></something>

      </Response>

</Body>

 

I have the following code:

 

[MessageContractAttribute(IsWrapped=false)]

public class Response

{

    [MessageBodyMember(Name = "Response", Namespace="https://tempuri.org", Order=0)]

    public ResponseBody body;

}

 

[DataContract(Namespace="https://tempuri.org")]

public class ResponseBody

{

    public ResponseBody(XmlNode[] s)

    {

        this.c = s;

    }

 

    [DataMember(EmitDefaultValue=false, Order=0)]

    public XmlNode[] c;

}

 

However, the code above would generate

 

<Body>

<Response>

<c>

<something></something>

</c>

</Response>

</Body>

In order to get rid of the extra element c, what i have to do is to define one class instead of two.

[MessageContractAttribute(IsWrapped=false)]

public class Response

{

[MessageBodyMember(Name = "Response", Namespace="https://tempuri.org", Order=0)]

public XmlNode[] c;

}

Hope it helps.