An Xml Serializable PropertyBag Dictionary Class for .NET

added by diamondz
10/9/2011 4:17:34 PM

192 Views

How to create a PropertyBag object which you can serialize to XML.


5 comments

fun_cyber
10/8/2011 9:56:19 AM
good thinking

dpeterson
10/10/2011 9:19:55 AM
It's a clever solution, but I'm wondering why XML should be used at all in this scenario? If the default serializer doesn't support IDictionary properly, why attempt to use it at all?
Using the JSON.NET library, dictionary objects can be serialized as JSON very easily. Dictionaries are easily represented in JSON, which is not the case with XML.

bradygaster
10/10/2011 11:56:34 AM
Would it be possible to extend this example in a way that JSON could be output rather than XML? I agree with you dpeterson, and think a lot of people will be alienated who are moving away from XML and into other serialization formats.

rstrahl
10/13/2011 4:55:36 PM
@dpeterson - actually I doubt you get two way serialization/deserialization of objects in JSON. It's not really possible given that no type information is available if you have Dictionary<string,object>. THere's no way for the deserializer to turn that back into the original dictionary unless type info is explicitly provided. There's more info on the post's comments about this.

dpeterson
10/13/2011 5:00:08 PM
Ah, I was working under the assumption that the object would be of a known type. Similar to how you would write code for mapping a DTO to your data layer and back. In this case you would just explicitly cast rather than storing any type information.