views:

232

answers:

1

I'm seeing some unusual behavior when using the DataContractSerializer. I have defined a message contract like so:

namespace MyNamespace.DataContracts
{
[MessageContract(WrapperName = "order", WrapperNamespace = @"http://example.com/v1/order")]
public class MyOrder
{
    [MessageBodyMember(Namespace = @"http://example.com/v1/order", Order = 1)]
    public MyStore store;

    [MessageBodyMember(Namespace = @"http://example.com/v1/order", Order = 2)]
    public MyOrderHeader orderHeader;

    [MessageBodyMember(Namespace = @"http://example.com/v1/order", Order = 3)]
    public List<MyPayment> payments;

    [MessageBodyMember(Namespace = @"http://example.com/v1/order", Order = 4)]
    public List<MyShipment> shipments;
}
.
.

I'm sending it an XML message that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<order xmlns="http://example.com/v1/order&gt;
    <store>
    ...
    </store>
    <orderHeader>
    ...
    </orderHeader>
    <payments>
        <payment>
        ...
        </payment>
    </payments>
    <shipments>
        <shipment>
        ...
        </shipment>
    </shipments>
</order>

My service deserializes this XML as expected. Inside my service, I'm using the DataContractSerializer to create an XML string and that's where things get weird. I'm using the serializer like this:

DataContractSerializer serializer = new DataContractSerializer(typeof(MyOrder));

using (MemoryStream ms = new MemoryStream())
{
    serializer.WriteObject(ms, order);                    
    ms.Position = 0;
    StreamReader sr = new StreamReader(ms);
    string outputMessage = sr.ReadToEnd();
}

Once this finishes, the outputMessage contains the following XML:

<?xml version="1.0" encoding="utf-8"?>
    <MyOrder xmlns="http://example.com/v1/order" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
        <order>
            <store>
            ...
            </store>
            <orderHeader>
            ...
            </orderHeader>
            <payments>
                <payment>
                ...
                </payment>
            </payments>
            <shipments>
                <shipment>
                ...
                </shipment>
            </shipments>
        </order>
    </MyOrder>

Needless to say, anything expecting to receive the original XML message will fail to parse this. So I guess I have two questions:

  1. Why is the DataContractSerializer adding the extra outer node to my XML output?
  2. Is there a way to stop it from doing this?

Thanks.

I should probably add this is with .NET 4.

A: 

You could try using WriteObjectContent instead of WriteObject, but I'm unable to reproduce your problem using the code you supplied. All the extra class defintions that are part of your message contract are empty in my definition, but this is the XML I am getting:

<MyOrder xmlns="http://schemas.datacontract.org/2004/07/SandboxApp"
         xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
    <orderHeader i:nil="true"/>
    <payments i:nil="true"/>
    <shipments i:nil="true"/>
    <store i:nil="true"/>
</MyOrder>

Which also seems odd, since it seems to ignore the WrapperName. Same result in .NET 3.5 SP1 and .NET 4.0.

Thorarin
The subelements in my MessageContract are all defined by classes with the DataContract/DataMember attributes.
sbanwart
I goofed up the paste in my original post. There is an extra "http://" in some of the namespaces. Try this instead. http://pastebin.com/FqBGVGLK
sbanwart
@sbanwart: I goofed up as well :) I had accidentally pasted all the classes inside my Program class... I'm now getting exactly the same result as in .NET 3.5.PS: slashes don't need escaping in regular strings, that's just backslashes. So no `@` needed.
Thorarin