在 C# 中解析大型 XML(大小为 1GB)的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/468948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:37:22  来源:igfitidea点击:

What is the best way to parse large XML (size of 1GB) in C#?

c#xmlparsing.net-2.0

提问by sivaramakrishna

I have a 1GB XML file and want to parse it. If I use XML Textreader or XMLDocument, the result is very slow and some times it hangs...

我有一个 1GB 的 XML 文件,想解析它。如果我使用 XML Textreader 或 XMLDocument,结果很慢,有时它会挂起......

回答by Spence

You'll have to implement custom logic using xmlreader. xmlreader does not load the full XML into memory before using it, which means you can read it from a stream and process it as such.

您必须使用 xmlreader 实现自定义逻辑。xmlreader 在使用之前不会将完整的 XML 加载到内存中,这意味着您可以从流中读取它并进行处理。

回答by pilif

XMLTextreader isn't supposed to hang as it's stream based and just works on chunks of the data.

XMLTextreader 不应该挂起,因为它是基于流的并且只处理数据块。

If it hangs, it may well be that you are doing something wrong when loading the file.

如果它挂起,很可能是您在加载文件时做错了什么。

回答by John K?llén

XmlDocument is not feasible in this scenario as it will attempt to suck that gigabyte into main memory. I'm surprised that you're finding XmlTextReader to be too slow. Have you tried something like the following?

XmlDocument 在这种情况下是不可行的,因为它会尝试将该 GB 字节吸入主内存。我很惊讶您发现 XmlTextReader 太慢了。您是否尝试过类似以下的操作?

using (XmlTextReader rdr = new XmlTextReader("MyBigFile.txt"))
{
     // use rdr to advance through the document.
}

回答by mafu

I'm not very familiar with this topic, but afaik the XmlReader-classes ought to work fine for your specific problem. They are, after all, optimized for exactly this.

我对这个主题不是很熟悉,但是我认为 XmlReader 类应该可以很好地解决您的特定问题。毕竟,它们正是为此而优化的。

回答by Presidenten

I would just like to back up everyone who promotes XmlReader with a performance comparison that I found:

我只想通过我发现的性能比较来支持所有推广 XmlReader 的人:

http://www.nearinfinity.com/blogs/joe_ferner/performance_linq_to_sql_vs.html

http://www.nearinfinity.com/blogs/joe_ferner/performance_linq_to_sql_vs.html