Web API 2 XML Serialization and DataContracts
<p>I have been getting errors when my Web API 2 solution tries to parse the result when XML is requested.</p>

<p>Come to find out through some more research. Dictionaries have issues when being parsed with the default XML serializer. </p>

<p>A property in a base class parses fine when it looks like this:</p>

<pre><code>public Dictionary&lt;string, CustomObject&gt; MyThings{ get; set; }
</code></pre>

<p>This works without all the DataContract and DataSerialization Attributes.</p>

<p>However, if I do the same thing but define the Value Type as 'object' I get the Error:</p>

<blockquote>
  <p>Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.</p>
</blockquote>

<p>Which is fine as from what I understand I need to decorate the class with expected types and the serializer should, in theory, serialize the object to XML.</p>

<p>When I decorate the class get the same error.</p>

<p>I don't know if this is because there is still limitations on being able to parse an 'object' type. But from what I have seen in other peoples issues and examples it works if say the property was of type;</p>

<pre><code>object 
</code></pre>

<p>instead of </p>

<pre><code>IDictionary&lt;string, object&gt;
</code></pre>

<p>I'm simply wondering if I am misunderstanding How to use the DataContract, KnownType and/or DataMember attributes or if this is actually still a limitation of parsing Dictionaries with the default XML Serializer. </p>

<p>I have gone through a number of examples and questions here but am coming up short on a good solution.</p>

<p>Has anyone come up with this in the past? If so, how did you get around the limitation?</p>

<p>I have tried using a List of KeyValuePairs but am also failing at that as well.</p>

<p>Thanks!</p>

<p>Example:</p>

<pre><code>    [DataContract]
    [KnownType(typeof(Dictionary&lt;string, Type_X&gt;))]
    [KnownType(typeof(Dictionary&lt;string, Type_Y&gt;))]
    public abstract class BaseClass
    {
        [DataMember]
        public Dictionary&lt;string, object&gt; BaseDictionary{ get; set; }

        public BaseClass()
        {
            BaseDictionary = new Dictionary&lt;string, object&gt;();
        }
    }
</code></pre>