Serializing Dictionaries to XML in C#

Unfortunately, objects instantiated from the Dictionary class can not be serialized to XML because it does not implement the IXmlSerializable interface. I will provide you with a couple of options to get around this problem.If you are not familiar with XML Serialization in C# you may want to read one of these tutorials first:

The Dictionary class is declared using the [SerializableAttribute] attribute so it can be serialized using a SOAP Formatter or a Binary Formatter as described in my post on Serializing objects using .Net’s SoapFormatter. The problem is that my current project requires me to serialize my data into simple XML and and the SoapFormatter is not a good option.

To overcome this obstacle, the best solution I found was to include Paul Welter’s XML Serializable Generic Dictionary in my project. I simply changed the Dictionary objects to be SerializableDictionary objects and didn’t have to modify any other code. Now my classes can simply be serialized to XML using the XmlSerializer Class.

About Stephen Curial

Co-founder of Pure Project Tools, that simplifies managing projects. I am currently a Software Development Engineer at Amazon and have worked as a developer on IBM's XLC Compiler, a government organization and a start-up company. In my free time I enjoy skiing, Brazilian Jiu-Jitsu (BJJ) and motorcycle racing. These views are my own and not that of my employer. View all posts by Stephen Curial

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: