序列化/反序列化字典的最佳方法是什么?< String,String& gt;在.Net 2.0中? [英] What's the best way to serialize / deserialize a Dictionary<String,String> in .Net 2.0?

查看:86
本文介绍了序列化/反序列化字典的最佳方法是什么?< String,String& gt;在.Net 2.0中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够将Dictionary序列化/反序列化为人类可读(如果可以归类为人类可编辑)字符串(最好是XML或Json)。



我只需要使用String字典,而无需使用任何其他类型的对象。
但是,这些字符串被假定为自由文本,因此它们可以包含单引号/双引号等字符,因此必须正确地对其进行编码/转义。



该项目基于.Net 2.0,因此很遗憾,我不能使用JSON.Net。



有什么想法可以快速完成此操作? p>

我显然可以编写自己的代码来做到这一点,但如果这样做的话,我希望会发现很多无法避免的麻烦,尤其是在反序列化方面。



任何想法都会受到赞赏。

谢谢!

Daniel

解决方案

据我所知,这种情况下现在存在三个简单可行的解决方案:



1)JSON.Net,确实可以工作在.Net 2.0中带有单独的二进制文件(使事情变得非常容易,并且json是一种不错的格式,比xml更为紧凑)。



2)可序列化字典的实现,例如网址:
http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx
(请注意,此版本不处理二进制文件由于缺少构造函数和缺少 [Serializable]属性,因此进行了序列化(BinaryFormatter),但这需要2分钟的修复时间-我希望作者可以更新该帖子!)



3) SharpSerializer 也使事情变得容易,尽管Xml的输出相当庞大默认值(尚未尝试使用设置)。



很抱歉这么晚才发布,但是我不确定为什么问题会一直悬而未决,我想这些答案中的任何一个都会对休闲搜索者有所帮助!



更新:显然我错过了一个, NXmlSerializer ,尽管我还没有玩过它-我不知道它与其他产品的比较。


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

解决方案

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.

这篇关于序列化/反序列化字典的最佳方法是什么?< String,String& gt;在.Net 2.0中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆