[英]What is the best way to parse large XML (size of 1GB) in C#?
I have a 1GB XML file and want to parse it. 我有一个1GB的XML文件,想解析它。 If I use XML Textreader or XMLDocument, the result is very slow and some times it hangs...
如果我使用XML Textreader或XMLDocument,则结果非常缓慢,有时会挂起...
You'll have to implement custom logic using xmlreader. 您必须使用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不会在使用前将完整的XML加载到内存中,这意味着您可以从流中读取它并对其进行处理。
XmlDocument is not feasible in this scenario as it will attempt to suck that gigabyte into main memory. XmlDocument在这种情况下不可行,因为它将尝试将该GB数据吸入主内存。 I'm surprised that you're finding XmlTextReader to be too slow.
我惊讶于您发现XmlTextReader太慢。 Have you tried something like the following?
您是否尝试过以下方法?
using (XmlTextReader rdr = new XmlTextReader("MyBigFile.txt"))
{
// use rdr to advance through the document.
}
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. 如果挂起,则很可能是您在加载文件时做错了什么。
I'm not very familiar with this topic, but afaik the XmlReader-classes ought to work fine for your specific problem. 我对这个主题不是很熟悉,但是afaik XmlReader类应该可以很好地解决您的特定问题。 They are, after all, optimized for exactly this.
毕竟,它们为此进行了优化。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.