C# and Reading Large XML Files

The answer to this question hasn't changed in .NET 4 - for best performance you should still be using XmlReader as it streams the document instead of loading the full thing into memory.

The code you refer to uses XmlReader for the actual querying so should be reasonably quick on large documents.


If it seems like this:

<root>
    <item>...</item>
    <item>...</item>
    ...
</root>

you can read file with XmlReader and each 'item' open with XmlDocument like this:

reader.ReadToDescendant("root");
reader.ReadToDescendant("item");

do
{
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(reader.ReadOuterXml());
    XmlNode item = doc.DocumentElement;

    // do your work with `item`
}
while (reader.ReadToNextSibling("item"));

reader.Close();

In this case, you have no limits on file size.

Tags:

C#

Xml