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

牧云@^-^@ 提交于 2019-12-10 20:35:43

问题


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#


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4164031/how-to-use-the-event-driven-saxsimple-api-for-xml-parser-in-c-sharp-or-is-it-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!