我如何/可以使用LINQ到XML的查询与合理的内存消耗巨大的XML文件? [英] How/Can I use linq to xml to query huge xml files with reasonable memory consumption?

查看:165
本文介绍了我如何/可以使用LINQ到XML的查询与合理的内存消耗巨大的XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有做什么用的LINQ to XML,但是我见过的所有示例加载整个XML文档到内存中。

I've not done much with linq to xml, but all the examples I've seen load the entire XML document into memory.

如果XML文件,说,8GB,你真的没有选择吗?

What if the XML file is, say, 8GB, and you really don't have the option?

我首先想到的是使用 XElement.Load方法(TextReader的)与组合 FileStream类的一个实例。

My first thought is to use the XElement.Load Method (TextReader) in combination with an instance of the FileStream Class.

问题:将这项工作,这是接近搜索一个非常大的XML文件的问题的正确方法?

QUESTION: will this work, and is this the right way to approach the problem of searching a very large XML file?

请注意:不要求高性能..我想获取的LINQ to XML来基本上做节目,我可以写通过我的大文件的每一行循环和收集起来的工作,但由于是LINQ 循环中心我希望这是可能的......

Note: high performance isn't required.. i'm trying to get linq to xml to basically do the work of the program i could write that loops through every line of my big file and gathers up, but since linq is "loop centric" I'd expect this to be possible....

推荐答案

使用 XElement.Load 将加载整个文件到内存中。相反,使用的XmlReader XNode.ReadFrom 的功能,在这里你可以选择加载由的XmlReader 与的XElement 作进一步处理,如果您需要。 MSDN有一个很好的例子正是这样做的:<一href=\"http://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.readfrom.aspx\">http://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.readfrom.aspx

Using XElement.Load will load the whole file into the memory. Instead, use XmlReader with the XNode.ReadFrom function, where you can selectively load notes found by XmlReader with XElement for further processing, if you need to. MSDN has a very good example doing just that: http://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.readfrom.aspx

如果你只需要搜索的xml文件,的XmlReader 单独就足够了,不会整个文件加载到内存中。

If you just need to search the xml document, XmlReader alone will suffice and will not load the whole document into the memory.

这篇关于我如何/可以使用LINQ到XML的查询与合理的内存消耗巨大的XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆