Friday, March 7, 2008

Serializable Dictionary

Some quick code for a VB.Net dictionary object which can be serialized to XML. Most of the credit goes to Paul Welter, who covers a C# implementation here.

This code is a port to VB with some minor structure changes. In Paul's example, each key/value pair is contained within <item> tags; in this version, the key is typed as a string and used as the tag. For our uses, this was simpler and resulted in slightly more readable XML output.

Imports System.Xml.Serialization

Public Class XmlDictionary(Of T)
Inherits Dictionary(Of String, T)
Implements IXmlSerializable

Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements IXmlSerializable.GetSchema
Return Nothing
End Function

Public Sub ReadXml(ByVal reader As System.Xml.XmlReader) Implements IXmlSerializable.ReadXml
Dim valSerializer As New XmlSerializer(GetType(T))

Dim wasEmpty As Boolean = reader.IsEmptyElement

If (wasEmpty) Then Return

While reader.NodeType <> Xml.XmlNodeType.EndElement

Dim key As String = reader.Name
Dim value As T = valSerializer.Deserialize(reader.ReadSubtree())

Me.Add(key, value)


End While


End Sub

Public Sub WriteXml(ByVal writer As System.Xml.XmlWriter) Implements IXmlSerializable.WriteXml

Dim valSerializer As New XmlSerializer(GetType(T))

For Each key As String In Me.Keys

Dim value As T = Me(key)
valSerializer.Serialize(writer, value)


End Sub

End Class

The resulting XML will look as follows:

<?xml version="1.0"?>

Note that the values are serialized and need not be strings. You may wish to check out Paul's code if you need non-string keys.

No comments: