开发者

WCF DataContractSerializer Behavior

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)]
    publ开发者_如何学编程ic 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>
    <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">
        <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.


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">
    <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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜