How to serialize/deserialize a Dictionary object in #CSharp?

Serialization and deserialization of .NET objects made easy using various serializer classes that it provides. But serialization of Dictionary object is not that much easy. For this, you have to create a special Dictionary class which is able to serialize itself. The serialization technique might be different in different business cases.


Today, let’s discuss how to implement the same with a sample. Code is shared in the post, which you can use in your application. Continue reading and let me know if you have any other approaches.


Dictionary serialization using C# (


To serialize a dictionary object, you need to first create a custom dictionary class, implementing IXmlSerializable interface. The interface contains three methods named GetSchema, ReadXml, WriteXml. You need to implement the logic in those methods. Below is the complete code for your reference:


public class LanguageSettings<TKey, TValue> : Dictionary<TKey, TValue>, 
    public XmlSchema GetSchema() { return null; }
    public void ReadXml(XmlReader reader)
        if(reader.IsEmptyElement) { return; }
        while (reader.NodeType != XmlNodeType.EndElement)
            object key = reader.GetAttribute("Title");
            object value = reader.GetAttribute("Value");
            this.Add((TKey)key, (TValue)value);
    public void WriteXml(XmlWriter writer)
        foreach (var key in this.Keys)
            writer.WriteAttributeString("Title", key.ToString());
            writer.WriteAttributeString("Value", this[key].ToString());


In the above code snippet you can observe that, we are serializing the object as XML attributes. In case you have a different business need, you can change it to root element type.


Once your custom dictionary class is ready, you can start writing your dictionary object as XML file. Below is the logic to implement the same with XmlSerializer API:


XmlSerializer serializer = new XmlSerializer(typeof(LanguageSettings<string, string>));
TextWriter textWriter = new StreamWriter(@"languages.xml");
serializer.Serialize(textWriter, settings);


You can also use XmlSerializer to read the XML and deserialize it to your dictionary object. You need to use TextReader to read the input stream from the XML file. Here’s the code snippet:


XmlSerializer serializer = new XmlSerializer(typeof(LanguageSettings<string, string>));
TextReader textReader = new StreamReader(@"languages.xml");
LanguageSettings<string, string> settings = 
                  (LanguageSettings<string, string>)serializer.Deserialize(textReader);


I hope that the post was useful and you would be able to use the shared code snippet in your project directly to serialize/deserialize a Dictionary object to/fro XML file. Please share your feedback about the post in the below comment section. If you have already implemented the same in different way, would love to see the same. Do share with us.


Connect with me on Twitter, Facebook and Google+ to get all the updates that I share over those social networking channels and keep yourself up to date on all technical fronts. Have a great day ahead.


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.


  1. This comment has been removed by a blog administrator.

  2. This comment has been removed by the author.

  3. Thanks for this nice post. How to serialize Dictionary which is having class object as a value.Thanks in this regards.

    1. Check out the Newtonsoft library. That will give you a better idea to do this.


© 2008-2016 - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top