对于公共 API,我应该使用什么 XML 序列化方法?

发布于 2024-10-03 09:22:44 字数 540 浏览 3 评论 0原文

我正在编写一个程序,该程序构建一个由继承自抽象 Node 类的类组成的树结构。我的程序中内置了许多不同类型的节点。但是,我还希望允许更高级的用户能够引用我的库并编写他们自己的 Node 派生。然后,当我的应用程序通过 Assembly.Load() 启动时,会加载这些插件库。因此,我的应用程序使用的所有潜在 Node 类型在运行时之前都是未知的。

此外,我希望能够将这些树与 XML 文件进行序列化和反序列化。我对 XMLSerializer、DataContractSerializer 和实现 IXmlSerialized 有一些经验。通常,我会使用 DataContractSerializer,因为它通常需要比实现 IXmlSerialized 更少的代码,并且可以序列化 XmlSerializer 无法序列化的私有字段。

然而,在这个项目中,我还必须考虑其他用户将创建从我的类派生的类,并且还必须添加序列化它们所需的任何代码或属性。

考虑到这一点,我是否应该选择一种序列化机制而不是另一种序列化机制?

I'm writing a program that builds up a tree structure made up of classes that inherit from an abstract Node class. There are a number of different type of nodes built into my program. However, I also want to allow more advanced users to be able to reference my library and write their own derivations of Node. These plug-in libraries are then loaded when my app starts up through Assembly.Load(). Thus all the potential Node types used by my application will not be known until run time.

In addition, I want to be able to serialize and deserialize these trees to and from XML files. I have some experience with XMLSerializer, DataContractSerializer, and implementing IXmlSerializable. Typically, I go with DataContractSerializer as it usually requires less code then implementing IXmlSerializable, and can serialize private fields where XmlSerializer can not.

Yet with this project I also have to consider that other users will be creating classes that derive from my class, and will also have to add whatever code or attributes are required to serialize them as well.

Considering this are there reasons I should go with one serialization mechanism over another?

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

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

发布评论

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

评论(1

淡淡の花香 2024-10-10 09:22:44

如果序列化和反序列化仅发生在您的应用程序中,并且不要求其他任何人都能够读取序列化数据,则序列化格式不会影响 API:就 API 用户而言,您将序列化为不透明文件并从该文件反序列化。

在这种情况下,请使用 DataContractSerializer,因为它可以在必要时序列化为二进制。

If the serialization and deserialization will only occur within your application, and if there is no requirement that anyone else be able to read the serialized data, then the serialization format doesn't impact the API: as far as a user of the API is concerned, you will serialize into an opaque file and deserialize from the same.

In this case, use DataContractSerializer, as it can serialize into binary if necessary.

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