.NET 中的速度和 XML 解析 - 序列化与 XML DOM 对比?

发布于 2024-07-21 23:58:40 字数 384 浏览 2 评论 0原文

我以前做过 XML 解析,但从未大规模进行过。 如果我正在处理许多类似于此格式的文档:

<?xml version="1.0" ?>
<items comment="something...">
  <uid>6523453</uid>
  <uid>94593453</uid>
</items>

解析这些文档的最快方法是什么?
1) XML DOM
2) XML 序列化 - 重新水化为 .NET 对象
3)其他一些方法

UPDATE
我忘了提到平均大约有 8000 个 uid 元素。

I have done XML parsing before but never on a massive scale.
If I'm working with many documents similar to this format:

<?xml version="1.0" ?>
<items comment="something...">
  <uid>6523453</uid>
  <uid>94593453</uid>
</items>

What is the fastest way to parse these documents?
1) XML DOM
2) XML Serialize - Rehydrate to a .NET Object
3) Some other method

UPDATE
I forgot to mention that there would be approx 8000 uid elements on average.

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

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

发布评论

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

评论(3

半枫 2024-07-28 23:58:40

使用 XmlReader 肯定会是最快的方法,尽管您当然必须手动完成所有解析。 它直接从流中读取,而不缓存任何内容,尽管与 DOM 相比使用起来不太方便。

比较您建议的两个:序列化应该比使用 DOM 更快,因为(我相信)它不会将整个树缓存在内存中 - 如果您专门旨在执行序列化,它当然也有一个更易于使用的界面。

Using XmlReader is definitely going to be the quickest method, though you'll have to do all the parsing manually of course. It reads directly from the stream without caching anything, though it's not too convenient to use compared to the DOM.

Comparing the two you suggested: serialisation ought to be quicker than using the DOM since (I believe) it doesn't cache the entire tree within memory - it also certainly has an easier to use interface, if you're specifically aiming to perform serialisation.

满身野味 2024-07-28 23:58:40

我想说 Xml 序列化将是两全其美的。 您将获得易用性和良好的速度。 xml 序列化会产生一些额外的开销...但是,如果您手动使用 XmlReader,那么当您使用该读取器重新创建对象图时,您至少会复制(如果不是超过)该开销。

I would say that Xml serialization would be the best of both worlds. You get ease of use, as well as good speed. There is some additional overhead with xml serialization...however if you used XmlReader manually, you will at least replicate, if not surpass, that overhead on your own as you use that reader to recreate your object graph.

将军与妓 2024-07-28 23:58:40

根据您需要对数据执行的操作,@Noldorin 提到的 XmlReader 是流式处理的最佳选择。 如果您需要更多临时样式的数据访问,那么使用 XPath 和 XPathDocument 将比原始 XML 文档快得多。

http://msdn.microsoft.com/en-us/library/eh3exdc4。 ASPX

Depending on what you need to do with the data the XmlReader mentioned by @Noldorin is your best bet for streaming style processing. If you need more ad-hoc style access to the data using XPath and the XPathDocument will be much faster than the raw XML document.

http://msdn.microsoft.com/en-us/library/eh3exdc4.aspx

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