Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading large XML documents in .net

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?

like image 483
Sam Avatar asked Oct 06 '11 08:10

Sam


People also ask

How do I view large XML files?

XML Explorer is lightweight and fast utility that allows you to view XML files. The best thing about this software is that it is able to handle huge XML files. The program has been tested even on files that are over 300 Mb.

How big can an XML file be?

There is no limit of XML file size but it takes memory (RAM) as file size of XML file, so long XML file parsing size is performance hit. It is advised to long XML size using SAX for . NET to parse long XML documents.


2 Answers

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.

like image 192
Jon Skeet Avatar answered Oct 12 '22 00:10

Jon Skeet


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

like image 44
spender Avatar answered Oct 12 '22 00:10

spender