简体   繁体   English

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

[英]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.

相关问题 如何以高效的方式编写1GB文件C# - How to write 1GB file in efficient way C# 在 C# 代码中解析(大)XML 的最佳方法是什么? - What is the best way to parse (big) XML in C# Code? .NET中用于将字符串搜索到最大1GB的大文件/文件夹中的最佳和最快的技术是什么? - What is the best and fastest technique in .Net for searchin a string into a large file/folder upto 1GB? 在C#中解析和上传> 1GB的数据 - Parsing and uploading >1GB of data in C# 在 C# 中解析此字符串的最佳方法是什么? - What is the best way to parse this string in C#? 如何在c#中通过TCP / IP发送大文件(500MB-1GB)? - How to send large file ( 500MB - 1GB ) over TCP/IP in c#? 如何快速创建具有“自然”内容的大型(> 1gb)文本+二进制文件? (C#) - How can I quickly create large (>1gb) text+binary files with “natural” content? (C#) 需要我的C#Winforms GUI应用程序加载和搜索巨大的xml(〜1GB) - Need my c# winforms gui application to load and search huge xml(~1GB) C#-解析xml之类的文本并执行操作的最佳方法 - C# - Best way to parse xml like text and perform action 获取C#中所有大于1gb的文件 - Obtain all files that are over 1gb in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM