問題描述
我需要使用 .net 文件讀取大型 xml,這些文件的大小很容易達到幾 GB.
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?
推薦答案
你基本上有在這里使用拉"模型 - XmlReader
和朋友.這將允許您流式傳輸文檔,而不是一次性將其全部加載到內存中.
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.
請注意,如果您知道自己處于足夠小"元素的開頭,則可以創建一個 XElement
來自 XmlReader
,使用 LINQ to 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 文檔的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!