如何在 C# 中使用事件驱动的 SAX(Simple API FOR XML) 解析器还是使用 System.XML 命名空间更好?

发布于 2024-10-01 18:32:43 字数 567 浏览 0 评论 0原文

在 C++ 中,我们可以将 msxml DLL 导入到我们的应用程序中,然后在我们应用程序的类中实现 ISAXContentHandler 接口的方法。稍后创建一个 ISAXReader 对象并使用此类的实例调用 contentHandler,我们在其中给出了事件处理程序接口的实现。
同样,我想在 C# 中实现同样的目标。有没有办法在 C# 中做到这一点?我的意思是有没有办法在 C# 中进行基于事件的 SAX 解析。 我认为还有一种方法可以帮助我实现解析 XML 文档并将 XML 信息获取到我的应用程序。 我点击了链接“http://www.codeguru.com/csharp/csharp/cs_data/xml/article.php/c4221”,

这里使用了 DOM 并使用了 System.XML 命名空间。 任何人都可以在这方面帮助我,----在 C# 中实现事件驱动的 XML 解析的方法是什么 或者我应该使用 System.XML 命名空间 (DOM)。如果存在 SAX 事件驱动方式,请告诉我如何在 C# 中做到这一点

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#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2024-10-08 18:32:43

我认为在 .Net 框架中最接近 SAX 阅读器的是 XmlReader 类。它不是基于事件的,但它允许您以类似的方式迭代文档。请看以下示例:http://msdn.microsoft。 com/en-us/library/system.xml.xmlreader.read.aspx

如果您需要最大性能,那么 XmlReader 是一个不错的选择,但还有其他替代方案可以提供更友好的 api。 XmlDocument 类允许您将 xml 文档视为层次结构树,而 XDocument 允许您使用 LINQ 解析 xml。您需要哪一种取决于您喜欢哪种型号以及您的要求。

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.

单调的奢华 2024-10-08 18:32:43

我前段时间遇到了类似的问题。虽然我无法回答哪个更好,是已经实现的 System.XML DOM 还是 C# SAX 解析器(这实际上取决于您的应用程序),但我可以告诉您,确实可以在 C# 中实现 SAX 解析器。

事实上,CodeProject 的 Martin Friedrich 已经完成了:走向声明式 SAX 框架:部分1 - 简单的 SAX 到 C# 映射

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文