XmlSerializer不能序列化Dictionary对象。。。

     异常:System.NotSupportedException : Cannot serialize member corp.Azure.DictProperties of type System.Collections.Hashtable, because it implements IDictionary.

    原来是因为Dictionary类型只实现的ISerializable接口,而没有实现IXmlSerializable接口,所以在.net 3.0以前用XmlSerializer是不能序列化的。在.net 3.0及以后版本可以使用DataContractSerializer进行序列化。

    一老外的详细解释:
   This question has come up a few times on internal discussion threads recently so I wanted to summarize some highlights. It's correct that you get this error because Dictionary isn't IXmlSerializable. The reason it's this way is layering concerns: Dictionary is in mscorlib.dll but IXmlSerializable is in System.Xml.dll, and we couldn't take a dependency in that direction.
   Before .NET 3.0 you'd have to grow your own solution, such as subclassing Dictionary to implement IXmlSerializable. Moving forward, the recommended solution is to use System.Runtime.Serialization.DataContractSerializer. Note that this is part of the Windows Communication Foundation (WCF), in .NET 3.0. For more details on what is serializable using the data contract serializer, see: http://msdn2.microsoft.com/en-us/library/ms731923.aspx

   --the end

posted on 2009-08-28 12:26  James.H.Fu  阅读(4496)  评论(3编辑  收藏  举报

导航