Reading large XML documents in .net(在 .net 中读取大型 XML 文档)
问题描述
我需要使用 .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 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 .net 中读取大型 XML 文档


基础教程推荐
- JSON.NET 中基于属性的类型解析 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01