在 C# 中解析大型 XML(大小为 1GB)的最佳方法是什么?
我有一个 1GB 的 XML 文件并且想要解析它。 如果我使用 XML Textreader 或 XMLDocument,结果会非常慢并且有时会挂起......
I have a 1GB XML file and want to parse it. If I use XML Textreader or XMLDocument, the result is very slow and some times it hangs...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须使用 xmlreader 实现自定义逻辑。 xmlreader 在使用之前不会将完整的 XML 加载到内存中,这意味着您可以从流中读取它并进行处理。
You'll have to implement custom logic using xmlreader. xmlreader does not load the full XML into memory before using it, which means you can read it from a stream and process it as such.
XmlDocument 在这种情况下不可行,因为它会尝试将这 GB 字节吸入主内存。 我很惊讶您发现 XmlTextReader 太慢了。 您是否尝试过类似以下的操作?
XmlDocument is not feasible in this scenario as it will attempt to suck that gigabyte into main memory. I'm surprised that you're finding XmlTextReader to be too slow. Have you tried something like the following?
XMLTextreader 不应该挂起,因为它是基于流的并且只处理数据块。
如果它挂起,很可能是您在加载文件时做错了什么。
XMLTextreader isn't supposed to hang as it's stream based and just works on chunks of the data.
If it hangs, it may well be that you are doing something wrong when loading the file.
我对这个主题不太熟悉,但据我所知,XmlReader 类应该可以很好地解决您的特定问题。 毕竟,它们正是针对这一点进行了优化。
I'm not very familiar with this topic, but afaik the XmlReader-classes ought to work fine for your specific problem. They are, after all, optimized for exactly this.
我只是想通过我发现的性能比较来支持所有推广 XmlReader 的人:
http ://www.nearinfinity.com/blogs/joe_ferner/performance_linq_to_sql_vs.html
I would just like to back up everyone who promotes XmlReader with a performance comparison that I found:
http://www.nearinfinity.com/blogs/joe_ferner/performance_linq_to_sql_vs.html