views:

556

answers:

3

I need to be able to serialize / deserialize a Dictionary to a human-readable (and if it comes down to it, human-editable) string (XML or Json preferably).

I only need to work with String dictionaries, not other object types of any kind. The strings, however, are presumed to be free text, so they can have characters like single/double quotes, etc, so they must be encoded/escaped correctly.

This project is based on .Net 2.0 so I can't use JSON.Net unfortunately.

Any ideas what's the best way to do this quickly?

I could obviously write my own code to do this, but I'm expecting to find a lot of escaping headaches if I go that way, especially on the deserializing part.

Any ideas will be appreciated.
Thanks!
Daniel

A: 

Implement IXmlSerializable, this will be very easy.

Pseudo code:

public class CoolHash : Dictionary<String, String>, IXmlSerializable
{
   void WriteXml(XmlWriter writer){
     foreach(string key in this.Keys){
        writer.WriteElementText(key, this[key]);
     }
   }
}
Mike Chaliy
Any pointers on how to implement the ReadXML part?
Daniel Magliola
The same way, XmlReader has all information, so you can easely navigate trough nodes and just set tag name as key and element value as value. You can find examples of reading XML files with XmlReader in XmlReader documentation.
Mike Chaliy
+1  A: 

I think the easiest way is to create a wrapper class that is easily serializable in the XML serializer and covertible to and from the Dictionary<string,string>. This will make it easily editable and removes all of the escaping issues from consideration.

For example

public class Wrapper { 
  public List<string> Keys {get; set; }
  public List<string> Values {get; set; }
  public Wrapper() {}
  public Wrapper(Dictionary<string,string> map) {
    Keys = new List<string>(map.Keys);
    Values = new List<string>(map.Values);
  }
  public Dictionary<string,string> GetMap() {
    Dictionary<string,string> map = new Dictionary<string,string>();
    for ( int i = 0; i < Keys.Length; i++ ) {
      map[Keys[i]] = Values[i];
    }  
    return map;
  }
}
JaredPar
A: 

As far as I know there are now three easy workable solutions this situation:

1) JSON.Net, which does work in .Net 2.0 with a separate binary (makes things REALLY easy, and json is a nice format, more compact than xml).

2) A Serializable Dictionary implementation, eg at: http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx (Please note this version does not handle Binary Serialization (BinaryFormatter) because of missing constructors and missing "[Serializable]" attribute, but this takes 2 minutes to fix - I wish the author would update the post!)

3) SharpSerializer, also makes things v easy, although the Xml output is pretty bulky by default (haven't tried playing with the settings).

Sorry to be posting this so late, but I'm not sure why the question languishes as unanswered and I think any of these answers would help the casual searcher!

UPDATE: Apparently I missed one, NXmlSerializer, although I haven't played with this one - I have no idea how it compares with the others.

Tao