XML Serializing a Hashtable or generic Dictionary

Published 09 April 05 12:11 PM | psheill 

The XmlSerializer in .NET has many good qualities.  It creates output that is understandable and not overly verbose.  It works with many data types.  It has a simple, sensible policy -- it only serializes public properties and members.  It doesn't require special attributes.  The only thing that bothers me is that it doesn't serialize Hashtables or Dictionaries.  Here is some code that enables serializing and deserializing those types by using an intermediary List.  I haven't tried it using a non-generic ArrayList or array, but I believe that would work too.

        using System.Collections.Generic;
    using System.Collections;
    using System.IO;
    using System.Xml.Serialization;
    using System.Xml;
    using System;
        public static void Serialize(TextWriter writer, IDictionary dictionary)
        {
            List<Entry> entries = new List<Entry>(dictionary.Count);
            foreach (object key in dictionary.Keys)
            {
                entries.Add(new Entry(key, dictionary[key]));
            }
            XmlSerializer serializer = new XmlSerializer(typeof(List<Entry>));
            serializer.Serialize(writer, entries);
        }
        public static void Deserialize(TextReader reader, IDictionary dictionary)
        {
            dictionary.Clear();
            XmlSerializer serializer = new XmlSerializer(typeof(List<Entry>));
            List<Entry> list = (List<Entry>)serializer.Deserialize(reader);
            foreach (Entry entry in list)
            {
                dictionary[entry.Key] = entry.Value;
            }
        }
        public class Entry
        {
            public object Key;
            public object Value;
            public Entry()
            {
            }
           
            public Entry(object key, object value)
            {
                Key = key;
                Value = value;
            }
        }

It generates output like the following, when the keys and values are strings.

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfEntry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Entry>
    <Key xsi:type="xsd:string">MyKey</Key>
    <Value xsi:type="xsd:string">MyValue</Value> 
  </Entry>
  <Entry>   
    <Key xsi:type="xsd:string">MyOtherKey</Key>   
    <Value xsi:type="xsd:string">MyOtherValue</Value> 
  </Entry>
</ArrayOfEntry>

 

Comments

# Matt Berther said on April 9, 2005 3:11 PM:
I've posted a similar technique [1] thats compatible with .NET 1.1 (ie: no generics), so yes, your assumption that it will work without generics is correct.

Secondly, I've also posted a solution [2] to remove the xsi and xsd declarations from the output xml, in case anyone's interested.

Enjoy!

[1] http://www.mattberther.com/2004/06/000487.html
[2] http://www.mattberther.com/2005/03/000603.html
# Vladimir Klisic said on May 11, 2005 9:30 AM:
(I apologize for jumping in so late, I just found this post over Google.)

Not so long ago I also came to the similar idea about using an intermediator between XmlSerializer and Hashtable, and ended up with a slightly different solution (framework v1.1):

http://blogs.metapropeller.com/klisa/PermaLink,guid,f70867c4-59a3-4a2b-b104-032b960c339d.aspx
# huseyint.com » XML Serializable Generic Dictionary tipi said on December 2, 2007 9:50 AM:

PingBack from http://huseyint.com/2007/12/xml-serializable-generic-dictionary-tipi/

# XML Serializable Generic Dictionary tipi | Turk Bili??im Teknolojileri Ve G??venlik Platformu | iyinet webmaster forumu 2008 seo yar????mas?? | said on March 25, 2008 6:09 PM:

PingBack from http://www.turkbt.com/wordpress/aspnet/xml-serializable-generic-dictionary-tipi/

# Serialize Hashtable to readable format? | keyongtech said on January 22, 2009 4:38 AM:

PingBack from http://www.keyongtech.com/673550-serialize-hashtable-to-readable-format

New Comments to this post are disabled
Page view tracker