The Dictionary (Of TKey, TValue) class is actually serializable, but in a binary sense. Unfortunately, the XmlSerialization mechanism cannot handle everything that implements IDictionary, and what prevents it from working in the application settings.
What I usually do for this is to create a small class that is XmlSerializable and represents a Key / Value pair. I think serializes a collection of these key / value pairs. It is easily converted back to the dictionary (Of TKey, TValue) later.
For example, if two elements were Name and Student
Public Class Pair ' Actual property implementation omited for brevity Public Property Student As Student Public Property Name As String End Class
Then I use the following to convert back and forth between an array and a dictionary
Public Function ToArray(map As Dictionary(Of Name,Student)) As Pair() Return map.Select(Function(x) New Pair(x.Key,x.Value).ToArray() End Function Public Function ToMap(arr As Pair()) As Dictionary(Of Name, Student) return arr.ToDictionary(Function(x) x.Name, Function(y) y.Student) End Function
source share