使用 C# 将大型 XML 文件处理到 SQL Server 中

发布于 2024-09-30 18:20:42 字数 462 浏览 0 评论 0原文

首先,我在这里描述的是已经存在的更大 ETL 流程的一小部分。因此,请不要建议移植到 SSIS 或其他环境,因为我做不到。

在此 ETL 过程中,对于要插入的 SQL Server 数据库中的每个表,我将:

  1. 将所有相关 xml 加载到 XElement 对象中
  2. ,然后将 xml 转换为类型化数据集数据表,
  3. 然后使用 SqlBulkCopy 对象快速插入数据写入sql server表。

但是,对于一个表,当我创建 XElement 时,我收到 OutOfMemory 异常。

因此,我现在需要以块的形式迭代处理数据,但我不确定执行此操作的最佳方法。 xml 文件存储在运行 ETL 过程的同一台计算机上。

感谢您的任何帮助。

更新

我开始阅读有关 XmlReader 类的内容,我从未使用过该类。如果有人认为这就是答案,请这么说并提供任何指导。

First, what I describe here is a small part of a larger ETL process that is already in place. So, please no suggestions to port to SSIS or some other environment because I can't.

In this ETL process, for each table in the SQL server database that is being inserted into, I am:

  1. loading all of the relevant xml into an XElement object
  2. then transforming the xml into a typed dataset datatable
  3. then using a SqlBulkCopy object to quickly insert the data into the sql server table.

But, for one table, when I create the XElement, I get an OutOfMemory exception.

So, I now need to iteratively process the data in chunks, but I'm not sure of the best way to do this. The xml file is stored on the same machine that is running the ETL process.

Thanks for any help.

UPDATE

I'm getting started reading about the XmlReader class, which I've never used. If someone thinks this is the answer, please say so and provide any guidance that you will.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不语却知心 2024-10-07 18:20:42

不要使用 XmlElement - 使用基于 .NET SAX 的解析器来解析 XML 流。永远不要具体化内存中的对象。简单的。有一个 API 可以实现这一点。

基本上,使用 XmlTextReader

Don't use XmlElement - use the .NET SAX based parser to parse the XML stream. NEVER materialize the objects in memory. Simple. There is an API for that.

Basically, use an XmlTextReader.

柠檬 2024-10-07 18:20:42

除了简单使用 XmlReader 之外,了解方法 XNode .ReadFrom。如果 XML 更像是一个很长的实体列表而不是深层嵌套的层次结构,那么它的效果尤其好。

In addition to plain use of XmlReader it could be useful to know about method XNode.ReadFrom. It works particularly well if XML is more like a very long list of entities as opposed to deep-nested hierarchy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文