tags:

views:

3965

answers:

4

I am attempting to read a large XML document and I wanted to do it in chunks vs XmlDocument's way of reading the entire file into memory. I know I can use XmlTextReader to do this but I was wondering if anyone has used SAX for .NET? I know Java developers swear by it and I was wondering if it is worth giving it a try and if so what are the benefits in using it. I am looking for specifics.

+5  A: 

If you're talking about SAX for .NET, the project doesn't appear to be maintained. The last release was more than 2 years ago. Maybe they got it perfect on the last release, but I wouldn't bet on it. The author, Karl Waclawek, seems to have disappeared off the net.

As for SAX under Java? You bet, it's great. Unfortunately, SAX was never developed as a standard, so all of the non-Java ports have been adapting a Java API for their own needs. While DOM is a pretty lousy API, it has the advantage of having been designed for multiple languages and environments, so it's easy to implement in Java, C#, JavaScript, C, et al.

Craig Trader
Hm, according to this page, SAX is a de facto standard in the industry (just not in the Microsoft world): http://www.xml.org/xml-dev
EnocNRoll
Oh, it might be worth noting that the official SAX implementation from Java is table and has been unmodified for even longer than SAX for .NET. The only time that improvements will be needed to either codebase is basically if the XML standard evolves still more.
EnocNRoll
+2  A: 

I believe there are no benefits using SAX at least due two reasons:

  1. SAX is a "push" model while XmlReader is a pull parser that has a number of benefits.
  2. Being dependent on a 3rd-party library rather than using a standard .NET API.
Greg
+4  A: 
EnocNRoll
+2  A: 

Personally, I much prefer the SAX model as the XmlReader has some really annoying traps that can cause bugs in your code that might cause your code to skip elements. Most code would be structured around a while(rdr.Read()) model, but if you have any "ReadString" or "ReadInnerXml()" within that loop you will find yourself skipping elements on the next iteration.

As SAX is event based this will never hapen as you can not perform any operations that would cause your parser to seek-ahead.

My personal feeling is that Microsoft have invented the notion that the XmlReader is better with the explanation of the push/pull model, but I don't really buy it. So Microsoft think that you don't need to create a state-machine with XmlReader, that doesn't make sense to me, but anyway, it's just my opinion.

Brett Ryan
Your opinion seems to be based on the fact that you learned a few things about `XmlReader` the hard way. Is that the best way to form an opinion on technical matters?
John Saunders
John, I suppose you're right, and I apologise. Though I do find that the XmlReader to be a fault of a lot of strange bugs in software that could be avoided by a simple SAX based approach.
Brett Ryan