将 XmlSerializer 替换为 BinarySerializer。我应该知道什么?

发布于 2024-11-06 11:03:47 字数 318 浏览 2 评论 0原文

我一直在使用 XmlSerializer 将我的 Project 类序列化到文件中。它里面包含了各种各样的信息。大多数工作都是由 XmlSerializer 自动完成的,除了一两个实现 IXmlSerialized 的类。现在我想切换到二进制序列化程序以使文件更小。我仍然不想序列化私有字段或类似的东西。我只是希望它的行为与 XmlSerializer 完全相同,但生成更小的文件。

有没有简单的方法可以做到这一点,或者我必须对我的应用程序进行大量更改?或者,我应该使用其他类型的序列化器吗?

I've been using an XmlSerializer all this time to serialize my Project class to a file. It contains all sorts of information in it. Most stuff is done automatically by the XmlSerializer, except for one or two classes that implement IXmlSerializable. Now I want to switch to a binary serializer only to make the files smaller. I still don't want to serialize private fields or anything like that. I just want it to behave exactly as an XmlSerializer but to generate smaller files.

Is there any easy way to do this, or will I have to make lots of changes to my application? Or, is there any other kind of serializer I should be using instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他是夢罘是命 2024-11-13 11:03:47

在我看来,切换到 BinaryFormatter 是一个坏主意 - 它从根本上改变了行为,特别是你信任版本之间和版本之间的文件的能力平台。这也意味着您现在正在序列化您的字段,这可能不是您想要序列化的数据的 1:1 映射(公共成员,默认)。

如果您想要更小,也许只需通过 DeflateStreamGZipStream 运行它。如果这还不够,我的下一步就是切换到 protobuf-net,因为它的行为与 XmlSerializer 非常相似(不相同),但作为二进制序列化器(而不是字段序列化器)。但是,它尊重IXmlSerialized - 根据复杂性,这可能很容易回避。

Switching to BinaryFormatter is a bad idea here, IMO - it radically changes the behaviour and in particular your ability to trust the files between versions and between platforms. It also means you are now serializing your fields, which may not be a 1:1 map to the data you wanted to serialize (the public members, by default).

If you want smaller, perhaps just run it through DeflateStream or GZipStream. If that isn't enough, my next move would be to switch to something like protobuf-net, for the reason that it behaves very similarly (not identically) to XmlSerializer, but as a binary serializer (not a field serializer). However, it does not respect IXmlSerializable - depending on the complexity, that may be easy to side-step.

﹂绝世的画 2024-11-13 11:03:47

恕我直言,缩小文件的最简单解决方案是采用当前 Office XML 格式(ODF 和 MS 格式)的方式:压缩它。这样,您就不必更改课程中的任何内容。

IMHO, the easiest solution to make your files smaller is to go the way the current Office XML formats (ODF and the one from MS) go: Zip it. This way, you don't have to change anything in your classes.

怪异←思 2024-11-13 11:03:47

如果您这样做只是为了空间,序列化后压缩 XML 文件是否可以作为替代方案?这样您就可以保持可读性,并且可以将文件命名为 project.xml.zip

If you do it only for space, might compressing the XML files after serialization be an alternative? This way you would keep readability and you could name your files project.xml.zip.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文