title: Custom serialisation of a Dictionary in a DataContract
body: <p>Basically I have a <code>DataContract</code> which contains a <code>Dictionary</code>:</p>
  
  <pre><code>[DataContract]
  public class MyDictionary : IDictionary&lt;string, object&gt; {
      [DataMember(Name = "Data")]
      protected IDictionary&lt;string, object&gt; Dictionary { get; private set; }
  
      // ...
  }
  </code></pre>
  
  <p>Here is the relevent part of the XML output:</p>
  
  <pre><code>&lt;Data&gt;
   &lt;a:KeyValueOfstringanyType&gt;
    &lt;a:Key&gt;ID&lt;/a:Key&gt;
    &lt;a:Value i:type="s:int"&gt;2&lt;/a:Value&gt;
   &lt;/a:KeyValueOfstringanyType&gt;
   &lt;a:KeyValueOfstringanyType&gt;
    &lt;a:Key&gt;Value&lt;/a:Key&gt;
    &lt;a:Value i:type="s:int"&gt;4711&lt;/a:Value&gt;
   &lt;/a:KeyValueOfstringanyType&gt;
  &lt;/Data&gt;
  </code></pre>
  
  <p>How can I simplify the output to something like this here:</p>
  
  <pre><code>&lt;Data&gt;
    &lt;ID i:type="s:int"&gt;2&lt;/ID&gt;
    &lt;Value i:type="s:int"&gt;4711&lt;/Value&gt;
  &lt;/Data&gt;
  </code></pre>
  
  <p>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 <code>CollectionDataContract</code> 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 <code>ISerializable</code> but I'm not sure if that makes some trouble with the <code>DataContractSerializer</code>. By the way the solution should also work with the <code>DataContractJsonSerializer</code>.</p>