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:
- XML Serialization in C#
- How to serialize an object to XML by using Visual C#
- Load and save objects to XML using serialization
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.