mingzckydowzky
mingzckydowzky

Reputation: 31

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 advice.

Upvotes: 2

Views: 4794

Answers (2)

topspin
topspin

Reputation: 401

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)

Upvotes: 4

Marcote
Marcote

Reputation: 3095

you need something like SAX but for .NET.

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

HTH

Upvotes: 0

Related Questions