.Net Tips – Xml Serialize or Deserialize Dictionary in C#

Yan Cui

I help clients go faster for less using serverless technologies.

If you try to serialize/deserialize a type which uses the generic Dictionary<TKey, TValue> type with the XmlSerializer then you’ll get an InvalidOperationException, for instance:

Here’s my class:

public class MyClass
{
    // need a parameterless constructor for serialization
    public MyClass()
    {
        MyDictionary = new Dictionary<string, string>();
    }
    public Dictionary<string, string> MyDictionary { get; set; }
}

I’ll get an InvalidOperationException with an inner exception of type NotSupportedException when I do this:

var xmlSerializer = new XmlSerializer(typeof(MyClass));

And the error message of the NotSupportedException is:

“Cannot serialize member MyClass.MyDictionary of type System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], because it implements IDictionary.”

The reason why Dictionary<TKey, TValue> is not supported by the XmlSerializer is that types such as Dictionary, HashTable, etc. needs an equality comparer which can’t be serialized into XML easily and won’t be portable anyhow. To get around this problem, you generally have two options, in the order of my personal preference:

Use DataContractSerizalizer

The easiest, and cleanest solution is to switch over to data contract serialization, and the only change required would be to mark your class with the DataContractAttribute and mark the properties you want to serialize with the DataMemberAttribute. My class would now look like this:

[DataContract]
public class MyClass
{
    // need a parameterless constructor for serialization
    public MyClass()
    {
        MyDictionary = new Dictionary<string, string>();
    }
    [DataMember]
    public Dictionary<string, string> MyDictionary { get; set; }
}

And to serialize it into Xml, just use the DataContractSerializer and write the output with a XmlTextWriter like this:

var serializer = new DataContractSerializer(typeof(MyClass));
string xmlString;
using (var sw = new StringWriter())
{
    using (var writer = new XmlTextWriter(sw))
    {
        writer.Formatting = Formatting.Indented; // indent the Xml so it's human readable
        serializer.WriteObject(writer, marketplace);
        writer.Flush();
        xmlString = sw.ToString();
    }
}

Use a custom Dictionary type

The alternative is to create your own Dictionary implementation which is Xml serializable. See the references section for an implementation Paul Welter has created.

References:

XML Serializable Generic Dictionary


 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


16 thoughts on “.Net Tips – Xml Serialize or Deserialize Dictionary in C#”

  1. theburningmonk

    @Nanek – imagine ‘marketplace’ being an instance of ‘MyClass’, looks like I missed out a line or two of the code snippet

  2. Thanks for the information.

    Just thought I’d add – you need to add a reference to System.Runtime.Serialization for this, and it is only available in .NET 3 upwards.

  3. Pingback: [C#] Dictionary ?? Xml « Develope note

  4. Great!
    I’d add that for Dat­a­Con­tract­Ser­iza­l­izer a reference to System.Runtime.Serialization is necessary.
    Thnaks, guy!

  5. In .Net 4.5.1 I am getting

    {“Type ‘System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]’ with data contract name ‘ArrayOfKeyValueOfstringstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays‘ is not expected. Consider using a DataContractResolver 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 DataContractSerializer.”}

  6. Here is the class I am trying to serialize

    [DataContract]

    public class KeywordFunctionMap

    {

    [DataMember]

    public Dictionary Map { get; set; }

    // need a parameterless constructor for serialization

    public KeywordFunctionMap()

    {

    Map = new Dictionary();

    }

    }

  7. Pingback: Serialising complex types in C#

  8. Pingback: ?????????? – CodingBlog

Leave a Comment

Your email address will not be published. Required fields are marked *