12

I need to read large xml using .net files which can easily be several GB of size.

I tried to use XDocument, but it just throws an System.OutOfMemoryException when I try to load the document.

What is the most performant way to read XML files of large size?

Sam
  • 28,421
  • 49
  • 167
  • 247
  • If you run on 64bit and want a quick fix for the OutOfMemoryException: go to your project properties -> build tab -> Platform target: change Any CPU to "x64". – BTC Aug 26 '15 at 09:13

3 Answers3

13

You basically have to use the "pull" model here - XmlReader and friends. That will allow you to stream the document rather than loading it all into memory in one go.

Note that if you know that you're at the start of a "small enough" element, you can create an XElement from an XmlReader, deal with that using the glory of LINQ to XML, and then move onto the next element.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

The following page makes an interesting read, providing a means to mine data from XML file without loading it in memory. It allows you to combine the speed of XmlReader with the flexibility of Linq:

http://msdn.microsoft.com/en-us/library/bb387035.aspx

And quite an interesting article based on this technique:

http://blogs.msdn.com/b/xmlteam/archive/2007/03/24/streaming-with-linq-to-xml-part-2.aspx

spender
  • 117,338
  • 33
  • 229
  • 351
0

You could try using an XmlTextReader instance.

http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.aspx

Daniel Becroft
  • 716
  • 3
  • 19