views:

99

answers:

2

I'm developing a modular app using prism in SL3, one of the modules is responsible for persisting the application settings in the isolated storage (so that when you open the app next time, you continue where you were). It works perfectly, except that I don't like the way dependencies are wired now.

I want to have a type-agnostic settings manager that has a generic store and then I add custom data from each module, some thing like this:

AppSettings["OpenForEditEmployees"] = new List<EmployeeDTO>();
AppSettings["ActiveView"] = ViewsEnum.Report;

I have implemented this part, but serialising that dictionary to xml proved to be harder than I suspected. I was wondering if there is an easy way to serialise a Dictionary<string, object> into XML.

+1  A: 

Have you looked at json.net http://json.codeplex.com/

It's not XML but it does a great job with serialization.

And, works great in Silverlight.

Klinger
+1  A: 

Since you are using a Dictionary, the regular XmlSerializer won't work, you can serialize using DataContractSerializer.

These 2 static classes will handle all of your serialization/deserialization needs for string representation of xml in silverlight (and any .NET)

You will need a reference to System.Runtime.Serialization for the DataContractSerializer

public static void SerializeXml<T>(T obj, Stream strm)
{
    DataContractSerializer ser = new DataContractSerializer(typeof(T));
    ser.WriteObject(strm, obj);
}

public static T DeserializeXml<T>(Stream xml)
{
    DataContractSerializer ser = new DataContractSerializer(typeof(T));
    return (T)ser.ReadObject(xml);
}

and if you would rather use JSON, you can add a reference to the System.ServiceModel.Web assembly and use this version instead.

public static void SerializeJson<T>(T obj, Stream strm)
{
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
    ser.WriteObject(strm, obj);
}

public static T DeserializeJson<T>(Stream json)
{
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
    return (T)ser.ReadObject(json);
}
Jason w