Basically I have a DataContract
which contains a Dictionary
:
[DataContract]
public class MyDictionary : IDictionary<string, object> {
[DataMember(Name = "Data")]
protected IDictionary<string, object> Dictionary { get; private set; }
// ...
}
Here is the relevent part of the XML output:
<Data>
<a:KeyValueOfstringanyType>
<a:Key>ID</a:Key>
<a:Value i:type="s:int">2</a:Value>
</a:KeyValueOfstringanyType>
<a:KeyValueOfstringanyType>
<a:Key>Value</a:Key>
<a:Value i:type="s:int">4711</a:Value>
</a:KeyValueOfstringanyType>
</Data>
How can I simplify the output to something like this here:
<Data>
<ID i:type="s:int">2</ID>
<Value i:type="s:int">4711</Value>
</Data>
The Dictionary key is restricted to string so if nobody get the stupid idea of using non ascii keys that should work fine. I found the attribute CollectionDataContract
with that I come a little bit closer to what I want but the key value pairs will be saved compleate which wasts memory. Maybe it is possible to slove with the class ISerializable
but I'm not sure if that makes some trouble with the DataContractSerializer
. By the way the solution should also work with the DataContractJsonSerializer
.