I've got a REST-ful WCF service that returns back an XML response. It is comprised of objects that are serializing and de-serializing correctly, with the one exception that my List property on a node is not deserializing correctly. The XML looks like:
<ShippingGroups>
<ShippingGroup>
<ShippingGroupId>
b0b4d8a4-ff1f-4f02-a47c-263ef8ac861b</ShippingGroupId>
<ShippingAddressId>
63c0b52c-b784-4c27-a3e8-8adafba36add</ShippingAddressId>
<LineItemIds xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:string>ccc0f986-52d5-453e-adca-8ff4513c1d84</a:string>
</LineItemIds>
</ShippingGroup>
The problem arises because my C# class to deserialize this XML is expecting a List LineItemIds. I can get around this by manually removing that namespace and removing the .
Is there another way around this, such that it would look like:
<ShippingGroups>
<ShippingGroup>
<ShippingGroupId>
b0b4d8a4-ff1f-4f02-a47c-263ef8ac861b</ShippingGroupId>
<ShippingAddressId>
63c0b52c-b784-4c27-a3e8-8adafba36add</ShippingAddressId>
<LineItemIds>
<string>ccc0f986-52d5-453e-adca-8ff4513c1d84</string>
</LineItemIds>
</ShippingGroup>
I think I have an answer for you. Without seeing your
DataContracts
, I'm kind of guessing as to what you're using and how your data is structured. But here goes...I am using VS2010, C#, WCF REST and .NET 4 for this.
By default, your collection or array is using a default namespace in order to maintain interoperability when it gets serialized. So, your serialization is behaving as designed.
You can get around this if you create a custom collection and use the
CollectionDataContract
attribute on it. You then have more control over how it gets serialized, including its namespace. Here's a detailed explanation about this from MSDN.So I created a custom collection and used the
CollectionDataContract
namespace as such:There are no properties since this collection will just hold strings.
I then have my
DataContract
that contains my custom string collection:Now that I've done that, I have my simple WCF RESTful service (GET):
When I request this service (http://localhost:xxxx/Service1/10), I get the following XML as a response:
Hopefully this helps. Please let me know if there are additional details that I missed or if there's more to the question. I'll update my answer accordingly.