将 XML 文件反序列化为 C# 类

发布于 2024-07-13 05:15:08 字数 62 浏览 3 评论 0原文

有谁知道使用 XSD 工具生成的类来探索反序列化的 XML 文件而不是 XPATH 有什么优势(内存/速度)?

Does anyone know what advantages (memory/speed) there are by using a class generated by the XSD tool to explore a deserialized XML file as opposed to XPATH?

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

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

发布评论

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

评论(3

通知家属抬走 2024-07-20 05:15:08

我想说的优点是你得到一个更方便使用的强类型类,而且如果文件中的 XML 数据对于创建对象无效,那么该类的构造函数将抛出异常,所以你得到一个免费最少的数据验证。

I'd say the advantage is that you get a strongly typed class which is more convenient to use, and also the constructor for the class will throw an exception if the XML data in the file is invalid for creating the object, so you get a minimal data validation for free.

紫南 2024-07-20 05:15:08

如果您不想编写样板代码,并且需要在执行过程中检查 XML 的任何值,那么使用 XSD.exe 生成的类就不会出错。

If you don't want to write boilerplate code, and you need to check ANY values of your XML on the way through, you can't go wrong with the XSD.exe generated classes.

沧笙踏歌 2024-07-20 05:15:08

两者有很大不同; 但 XmlSerializer 始终会反序列化整个对象; 使用 XPath,您可以挑选。 不过,我个人会使用 XmlSerializer - 更难出错。

然而,XPath 是一个依赖于后端的复杂野兽。 例如,XmlDocument(可变)的行为与 XPathDocument(只读,针对查询进行了优化)不同。

The two are very different; but XmlSerializer will always deserialize entire objects; with XPath you can pick and choose. I'd use XmlSerializer personally, though - harder to get wrong.

XPath, however, is a complex beast that depends on the back-end. For example, XmlDocument (mutable) will behave differently to XPathDocument (read-only, optimized for query).

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