Why can't a Dictionary<key,value> be serialized? Or can it?

10,931

I think this is just a limitation of Microsoft's XML serializer (System.Xml.Serialization.XmlSerializer). Most other serializers will support dictionaries.

This is not a general limitation of serialization, not even xml serialization, just a limitation of that implementation. XmlSerializer is pretty weak in general.

Share:
10,931
Charlie Kou
Author by

Charlie Kou

Updated on June 04, 2022

Comments

  • Charlie Kou
    Charlie Kou almost 2 years

    (Havent spotted a question asking this).

    Why cant a Dictionary be serialized?

    Most resources, websites, blogs etc say that it cannot be serialized. However reading "CLR via C#" 3rd edition, page 664 gives Dictionary as an example of an object graph which can be serialized.

    Note that this chapter talks about Binary Serialization. So is it that it can be serialized using the BinaryFormatter but it cannot be XML serialized?

    Or is there some difference here that Im missing between an IDictionary and a Dictionary?

    To clarify... under what circumstances can a Dictionary be serialized and under what circumstances can it not be serialized.

    Thanks.

  • Charlie Kou
    Charlie Kou over 12 years
    Ok, interesting - any ideas why the limitation is in place?