Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF DataContractSerializer Behavior
    text
    copied!<p>I'm seeing some unusual behavior when using the DataContractSerializer. I have defined a message contract like so:</p> <pre><code>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&lt;MyPayment&gt; payments; [MessageBodyMember(Namespace = @"http://example.com/v1/order", Order = 4)] public List&lt;MyShipment&gt; shipments; } . . </code></pre> <p>I'm sending it an XML message that looks like this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;order xmlns="http://example.com/v1/order&gt; &lt;store&gt; ... &lt;/store&gt; &lt;orderHeader&gt; ... &lt;/orderHeader&gt; &lt;payments&gt; &lt;payment&gt; ... &lt;/payment&gt; &lt;/payments&gt; &lt;shipments&gt; &lt;shipment&gt; ... &lt;/shipment&gt; &lt;/shipments&gt; &lt;/order&gt; </code></pre> <p>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:</p> <pre><code>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(); } </code></pre> <p>Once this finishes, the outputMessage contains the following XML:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;MyOrder xmlns="http://example.com/v1/order" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt; &lt;order&gt; &lt;store&gt; ... &lt;/store&gt; &lt;orderHeader&gt; ... &lt;/orderHeader&gt; &lt;payments&gt; &lt;payment&gt; ... &lt;/payment&gt; &lt;/payments&gt; &lt;shipments&gt; &lt;shipment&gt; ... &lt;/shipment&gt; &lt;/shipments&gt; &lt;/order&gt; &lt;/MyOrder&gt; </code></pre> <p>Needless to say, anything expecting to receive the original XML message will fail to parse this. So I guess I have two questions:</p> <ol> <li>Why is the DataContractSerializer adding the extra outer node to my XML output?</li> <li>Is there a way to stop it from doing this?</li> </ol> <p>Thanks.</p> <p>I should probably add this is with .NET 4.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload