How to use the event driven SAX(Simple API FOR XML) parser in C# or is it better to use System.XML namespace?

StackOverflow https://stackoverflow.com/questions/4164031

سؤال

In C++ we can import msxml DLL to our application and then implement the methods of the interface ISAXContentHandler in a class of our application. Later create an ISAXReader object and invoke the contentHandler with the instance of this class where we have given the implemnatation for the eventhandler interfaces.
Similiarly I want to achieve the same thing in C#. Is there any way to do this in C#?? I mean is there any way to have eventbased SAX parsing in C#. There is one more method I thought would help me in achieving parsing the XML documant and getting the XML information to my application. I followed the link "http://www.codeguru.com/csharp/csharp/cs_data/xml/article.php/c4221"

Here DOM is used and System.XML namespace is being used. Can anyone kindly help me with this regard,----What is the way to achieve Event driven XML parsing in C# or should I use System.XML namespace (DOM). if there exists a SAX event driven way plz let me know how to do it in C#

هل كانت مفيدة؟

المحلول

I think the closest you get to a SAX-reader in the .Net framework is the XmlReader class. It is not event based, but it allows you to iterate through the document in a similar manner. Look at this example: http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.read.aspx

If you need maximum performance then the XmlReader is a good choice, but there are other alternatives that offer a more friendly api. The XmlDocument class allows you to treat the xml document as a hierarchical tree, while the XDocument allows you to use LINQ to parse the xml. Which one you need depends on which model you are comfortable with and your requirements.

نصائح أخرى

I ran into a similar problem some time ago. While I can't answer which is better, the already implemented System.XML DOM or a C# SAX parser (this really depends on your application), I can tell you that it is indeed possible to implement a SAX parser in C#.

In fact, it has already been done by Martin Friedrich at CodeProject: Towards a Declarative SAX Framework : Part 1 - A Simple SAX-to-C#-Mapping.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top