开发者

How to resolve System.OutOfMemoryException when loading large XML file

I have this code on my program that actually loads 500 MB and up files.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
reader.Close();

I get this kind of error and don't know how to resolve the problem. Please send me some adv开发者_如何学编程ice.


I would use an XmlReader to parse the document, providing forward only access to the data and cleans itself up nicely in memory -- of course, it can be much more complex without the convenience of the XmlDocument class.

This simple sample will start by starting to read the file line by line, providing an XmlReader for each line.

using (var rdr = XmlReader.Create(new StreamReader("File.xml")))
{
    while (rdr.Read())
    {
        //do what you will with the line
    }
}

See the methods and properties available to you when using the XmlReader at XmlReader Properties (MSDN)


you need something like SAX but for .NET.

http://sourceforge.net/projects/saxdotnet/ or the XmlReader, basically a stream based parser.

HTH

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜