问题描述
我需要使用.net文件,这很容易被几个GB大小的读取大的XML。
I need to read large xml using .net files which can easily be several GB of size.
我试图用的XDocument,但它只是抛出一个System.OutOfMemoryException的当我尝试加载文档。
I tried to use XDocument, but it just throws an System.OutOfMemoryException when I try to load the document.
什么是最高效的方式来读取大型XML文件?
What is the most performant way to read XML files of large size?
推荐答案
您基本的有无的使用拉模式在这里 - 的和朋友。这将让你流的文件,而不是装载这一切到内存中一气呵成。
You basically have to use the "pull" model here - XmlReader
and friends. That will allow you to stream the document rather than loading it all into memory in one go.
请注意,如果你知道你在一个足够小元素的开始,你可以创建一个的,处理,使用LINQ到XML的荣耀,然后移动到下一个元素。
Note that if you know that you're at the start of a "small enough" element, you can create an XElement
from an XmlReader
, deal with that using the glory of LINQ to XML, and then move onto the next element.
这篇关于阅读在.net中大型XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!