3

What is the best way to process XML if I can't afford to load the whole file into memory?

Dmitrii Lobanov
  • 4,897
  • 1
  • 33
  • 50
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
  • 1
    In general terms, a "SAX parser" processes the file as it's read, while a "DOM parser" loads the whole document. http://en.wikipedia.org/wiki/Simple_API_for_XML#Benefits – Tim Sylvester Apr 05 '11 at 04:54

2 Answers2

9

Use an XmlReader and process element by element.

Justin
  • 84,773
  • 49
  • 224
  • 367
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • 1
    +1 This is the right idea. Note however, that `XmlReader` is an abstract class. If you are reading character data, you probably want to use an `XmlTextReader`. See: http://msdn.microsoft.com/en-us/library/system.xml.xmlreader(v=VS.100).aspx – Joel Lee Apr 05 '11 at 04:25
1

You can use XmlReader class, it doesn't load the whole document into memory. Here you can find the complete list of ways to deal with XML with examples and pros and cons for every technology used.

Community
  • 1
  • 1
Dmitrii Lobanov
  • 4,897
  • 1
  • 33
  • 50