什么是序列化/反序列化字典< String,String>在.Net 2.0? [英] What's the best way to serialize / deserialize a Dictionary<String,String> in .Net 2.0?

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

问题描述

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



我只需要使用String字典,而不是任何类型的其他对象类型。
然而,字符串被推定为自由文本,因此它们可以具有单/双引号等字符,因此必须正确编码/转义。



这个项目是基于.Net 2.0,所以我不能使用JSON.Net不幸的是。



任何想法,什么是最快的方法这样做? p>

我可以明白地编写自己的代码来做到这一点,但是如果我这样做,特别是在反序列化部分,我希望找到很多逃脱的头痛。 p>

任何想法将不胜感激。

谢谢!

丹尼尔

解决方案

据我所知,现在有三种简单可行的解决方案:



1)JSON.Net在.Net 2.0中有一个单独的二进制(使事情真的很简单,json是一个不错的格式,比xml更紧凑)。



2)可序列化字典实现,例如at:
http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx
(请注意这个版本不会处理二进制序列化(BinaryFormatter),因为缺少构造函数和缺少[Serializable]属性,但是这需要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>在.Net 2.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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