C# 替换 XML 序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1186743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replacement for XML Serialization
提问by Matthew Scharley
I have code using XmlSerializer
to serialize/deserialize a data structure for persistance. I've read and heard in several places here on StackOverflow that XmlSerializer
is one or more of:
我有代码XmlSerializer
用于序列化/反序列化数据结构以实现持久性。我在 StackOverflow 上的几个地方读过和听到过,XmlSerializer
其中一个或多个:
- Bad
- Poorly supported/implemented
- Possibly won't be supported into the future
- 坏的
- 支持/实施不佳
- 将来可能不会支持
My question is two-fold; is any of the above true, and if so, what alternatives exist? For my purposes, XML works very well, and I would like to keep that much constant, but everything else is open to change.
我的问题有两个方面;以上是否属实,如果是,还有哪些替代方案?就我的目的而言,XML 工作得非常好,我希望保持这一点不变,但其他一切都可以改变。
EDIT:If you want to suggest something other to XML, I'm open to it, but it does need to be human-readable.
编辑:如果你想对 XML 提出其他建议,我愿意接受它,但它确实需要是人类可读的。
采纳答案by Marc Gravell
XmlSerializer
is perfectly supportable, but has some glitches;
XmlSerializer
完全可以支持,但有一些小故障;
- relativelyslow; but usually this is still fast enough
- only supports public members; can be a pain
- requires write accessors lists - just ugly
- 比较慢;但通常这仍然足够快
- 只支持公共成员;可能是一种痛苦
- 需要写访问器列表 - 只是丑陋
However, I expect it to continue to be there for a considerable time; IMO, it is BinaryFormatter
that has the realproblems (when used for persistence).
但是,我预计它会在相当长的一段时间内继续存在。国际海事组织,它是BinaryFormatter
一个具有现实问题(用于持久性时)。
I'm very biased (since I'm the author), but I'd choose protobuf-net; a binary serializer using Google's "protocol buffers" wire format; fast, portable between languages/platforms, very small output, version tolerant, etc (and free, of course). Clearly not xml, though - so not human readable.
我非常有偏见(因为我是作者),但我会选择protobuf-net;使用 Google 的“协议缓冲区”有线格式的二进制序列化程序;快速,语言/平台之间的可移植性,非常小的输出,版本容忍等(当然是免费的)。显然不是 xml,虽然 - 所以不是人类可读的。
回答by womp
If you can use .Net 3.5 (preferably SP1), I'd look into the DataContractSerializer. Although it is less configurable than the XmlSerializer, it's faster, easier to work with (at least in my experience) and more portable (i.e. for web services). SP1 changed the default behaviour of it to be opt-out, so you can serialize any class without having to explictly define attributes on everything you need to serialize.
如果您可以使用 .Net 3.5(最好是 SP1),我会查看DataContractSerializer。尽管它的可配置性不如 XmlSerializer,但它更快、更容易使用(至少在我的经验中)并且更便携(即对于 Web 服务)。SP1 将其默认行为更改为选择退出,因此您可以序列化任何类,而无需在需要序列化的所有内容上明确定义属性。
I would recommend thoroughly reading the documentation on it though before investing in it as a change. Depending on how customized your serialization is, it may not work for you.
我建议在投资它作为改变之前彻底阅读它的文档。根据您的序列化的定制程度,它可能不适合您。
回答by user145586
jSON is much more faster than XML. You can use Json.NET to read it. It has built-in serialization.
JSON 比 XML 快得多。您可以使用 Json.NET 来阅读它。它具有内置的序列化。
回答by John Saunders
As far as the XML Serializer, there's "supported", and there's "supported".
至于 XML 序列化程序,有“支持”和“支持”。
An increasing number of Connect bug reports on the XML Serializer are coming back acknowledging bugs, and stating that the bugs will not be fixed.
越来越多的关于 XML 序列化程序的 Connect 错误报告回来承认错误,并声明这些错误不会被修复。
I'm sure that if you ran into a security-critical bug in the XML Serializer, that it would be fixed. However, I think it's unlikely that other bugs that are not as critical will ever be fixed.
我敢肯定,如果您在 XML Serializer 中遇到一个安全关键的错误,它会被修复。但是,我认为不太可能修复其他不那么严重的错误。
回答by Cheeso
I'm going to take an alternative view:
XmlSerializer is supported, its behaviors are known, and it works well. It is not "bad". It is very general, well documented, has lots of examples. The performance is probably very good for what you need. It probably does what you need.
我将采取另一种观点:
支持 XmlSerializer,它的行为是已知的,并且运行良好。这不是“坏”。它非常通用,有据可查,有很多例子。性能可能非常适合您的需要。它可能会满足您的需求。
There are some people who have particular needs that are not addressed by the XmlSerializer. From those requirements we get things like protobufs, DataContractSerializer, and other options.
有些人有 XmlSerializer 没有解决的特殊需求。从这些需求中,我们得到了诸如 protobufs、DataContractSerializer 和其他选项之类的东西。
But XmlSerializer is still very general and probably the most widely applicable serializer in town. It's still probably the safest bet for serializing content.
但是 XmlSerializer 仍然非常通用,并且可能是镇上应用最广泛的序列化器。它仍然可能是序列化内容最安全的选择。
As to support...
MS may be slowing down on fixing bugs. I compare this to WinForms. WinForms is no longer the primary UI framework being pushed by Microsoft. But it is still mature, works well, performs well. XmlSerializer is the same.
至于支持...
MS 可能会放慢修复错误的速度。我将其与 WinForms 进行比较。WinForms 不再是微软推出的主要 UI 框架。但它仍然成熟,运行良好,性能良好。XmlSerializer 也是一样的。
As for support into the future. MS has a 5+5 support policy - they support a product for 5 years after its release, and then you can buy 5 years additional support for it. The .NET Framework is not the supported "thing" - it is the Windows OS that includes .NET that is supported. Windows 7 will include .NET 3.5 (I think the version is 3.5?) and so everything in .NET 3.5, including WinForms and XmlSerializer, will be "officially supported" for an additional 5 years, starting in October or whenever Win7 is released. If it is .NET 4.0, then anything in 4.0 (including, still, WinForms and XmlSerializer) will be supported for 5 years. The 5-year clock restarts every time a new product ships with .NET.
至于未来的支持。MS 有 5+5 支持政策 - 他们在产品发布后支持 5 年,然后您可以为它购买 5 年的额外支持。.NET Framework 不是受支持的“东西”——它是包含受支持的 .NET 的 Windows 操作系统。Windows 7 将包含 .NET 3.5(我认为版本是 3.5?),因此 .NET 3.5 中的所有内容,包括 WinForms 和 XmlSerializer,将在 10 月或 Win7 发布时再“正式支持”5 年。如果是 .NET 4.0,那么 4.0 中的任何内容(包括,仍然,WinForms 和 XmlSerializer)都将得到 5 年的支持。每次新产品随 .NET 一起发布时,5 年时钟都会重新开始。
Looking at the VB6 Runtime, it was originally shipped with Visual Studio 6, in 1998. It has been included in Windows since then, including Windows Server 2008 R2, released this year. So the VB runtime will be supported at least until 2014. That's 16 years of mainstream support, at least.
看看 VB6 运行时,它最初是在 1998 年随 Visual Studio 6 一起提供的。从那时起它就包含在 Windows 中,包括今年发布的 Windows Server 2008 R2。因此,VB 运行时将至少支持到 2014 年。至少,这是 16 年的主流支持。
You have nothing to worry about as far as official support. This isn't an open-source project you're talking about. This isn't a stopgap offering like WSE or the SOAP Toolkit.
就官方支持而言,您无需担心。这不是您所说的开源项目。这不是像 WSE 或 SOAP Toolkit 那样的权宜之计。
It's true that there are degrees of support, and older .NET APIs ratchet down in priority as the newer ones are launched and promoted. But the older ones are presumably more stable, by the time they plateau. You're totally safe.
确实存在一定程度的支持,并且随着新 API 的推出和推广,较旧的 .NET API 的优先级逐渐降低。但是,当它们达到稳定状态时,年龄较大的可能更稳定。你是完全安全的。
回答by MatthewMartin
Here is a library that replaces the XmlSerializer:
这是一个替换 XmlSerializer 的库: