'What is the best way to work with xml?

what is the best way to work with xml file that represets a tree. the xml size is 70mb.



Solution 1:[1]

Linq to XML is the easiest way to currently work with xml but this will typically load the entrire tree into memory which in your case with a 70mb file may not be ideal.
However there are ways around this as demonstrated in this blog post from Mick Taulty.

Solution 2:[2]

The answer depends on what you want to do with the XML. Generally with files that size you wouldn't want to read it all in one go. As such the following page makes an interesting read, providing a means to mine data from the 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:

Link

Solution 3:[3]

If you want to read data from a large xml file XmlTextReader is the way to go.

Solution 4:[4]

For .NET 3.5 and up, I prefer using LINQ to XML for all my work towards XML files.

Solution 5:[5]

LinqToXml is probably a good bet if you wish to query it in memory, but if you find that you are getting problems with how large your memory footprint is you could use an XMLReader

Linq To XML

  • Slower for larger documents (large memory footprint)
  • Queryable

XmlTextReader

  • Fast
  • Only one line at a time, so no querying

Solution 6:[6]

Since you are already using a DOM, an alternative XML parser you could try is a SAX parser. Instead of loading the entire tree into memory, a SAX parser is event-driven and handles nodes, etc. as it encounters them.

Further Reading: http://www.saxproject.org/event.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andy Rose
Solution 2 Glorfindel
Solution 3 Stefan P.
Solution 4 Øyvind Bråthen
Solution 5 Pondidum
Solution 6 matt.schechtman