Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 类型为';的未处理异常;System.OutOfMemoryException';发生在System.Xml.dll中_C#_.net_Xml_Out Of Memory_Xmldocument - Fatal编程技术网

C# 类型为';的未处理异常;System.OutOfMemoryException';发生在System.Xml.dll中

C# 类型为';的未处理异常;System.OutOfMemoryException';发生在System.Xml.dll中,c#,.net,xml,out-of-memory,xmldocument,C#,.net,Xml,Out Of Memory,Xmldocument,我有以下代码: XmlTextReader DBLPReader = new XmlTextReader("dblp.xml"); // Load the source of the XML file into an XmlDocument XmlDocument DBLPDoc = new XmlDocument(); // Load the source XML file into the first document DBLPDoc.Load(DBLPReader); // Clos

我有以下代码:

XmlTextReader DBLPReader = new XmlTextReader("dblp.xml");

// Load the source of the XML file into an XmlDocument
XmlDocument DBLPDoc = new XmlDocument();

// Load the source XML file into the first document
DBLPDoc.Load(DBLPReader);

// Close the reader
DBLPReader.Close();`
其中
dblp
大约有800MB

我收到一个错误,上面写着“System.Xml.dll中发生了类型为'System.OutOfMemoryException'的未处理异常”


这种情况下的解决方案是什么?

根据您试图实现的目标,您的解决方案可能是:

  • 使用SAX解析器
  • 使用64位机器
  • 不要试图加载这么大的文件
  • 使文件更小(例如,消除空白或注释,缩短标记名)
  • 捕获异常并正确处理它

如果你要用这个大小的XML文件乱搞,你应该考虑使用。这里有一个列表,列出了它将带给你什么,还有一个关于如何使用它的不错的教程。

在64位机器上运行,并将构建属性设置为针对x64平台

实际上。。正如您所看到的,我正在使用XMLTextReader,但是有没有其他的编码方法可以帮助我使用XMLTextReader。。regards@Dena,只使用XmlReader/XmlTextReader,不要将其加载到XmlDocument中。谢谢@Simon——这就是我的意思@Dena:XmlDocument读取整个XML文件,并在内存中构建一个DOM树,您可以对其进行编码。即使您认为他们的内存开销非常高,但它仍然至少与原始文件一样大(这可能是非常慷慨的)。我将从我提到的教程开始:)SAX本身并不一定比DOM好。在使用一种XML解析方式与使用另一种解析方式之间存在权衡。也就是说,SAX风格的解析器往往更快、更轻(如果使用起来更复杂一些的话)。在这种情况下,SAX可能更好,因为您可以避免DOM的所有内存开销(这显然是问题所在),并且只存储需要从文件中提取的数据。