XPath 和 Linq2Xml 哪个更好
在 xml 中搜索的“最佳”方式是什么?
Xpath 或 Linq2xml。
我问这个是因为我们需要在 xml 中进行大量搜索。
我总是使用 XPath(因为我从 .NET 1.1 开始就使用它)。但随着 Linq 的引入,您可以轻松地使用 Linq2Sql。
问候, M。
What is the "best" way to search in xml?
Xpath or Linq2xml.
I'm asking this because we need to do a lot of searching in xml.
I'm always using XPath (since I've been using this from .NET 1.1). But with the introduction of Linq you can easely use Linq2Sql.
Regards,
M.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也广泛使用 xsl,
它们有非常不同的用途,我认为
xpath 非常适合操作 xml 文档,而 linq2xml 非常适合将它们映射到对象集合中。
换句话说,我经常有涉及两者的应用程序。
例如,将 csv 解析为给定的 xml 结构几乎是 xslt 和 xpath 的首选,而如果您有一个包含可选元素的 xml 文档,linq2XMl 会给您带来问题。所以我倾向于使用 xpath 来真正锁定 xml 格式,使其明确并保持我的 linq2xml 映射非常非常简单。
结果是错误大大减少,开发速度加快。
不知道为什么这个人在谈论 linq2xsd ...它是一个已停止的项目,文档非常少。远离它。
Xdocument 是一个使用起来非常愉快的对象... xmldocument 在我看来只是一个繁琐的对象。显然这取决于手头的任务,但是缺少 xpath 2.0 让我倾向于将其用作数据清理器,然后让 linq2XMl 来做真正的工作。
就搜索而言,您可以执行 linq2xml 在 xpath 中执行的所有操作,问题是从语法上讲,我更喜欢使用 linq2sql 并使用强类型集合,而不是使用 xpath。稍后再回来适应会容易得多。另外,您不必担心 xpath 实现之间的语法差异,尤其是 regex 实现之间的语法差异
i use both extensively also xsl
they have very different uses imo
xpath is great for manipulating xml documents wheras linq2xml is great for mapping them into object collections.
In other words i regularly have applications that involve both.
for instance parsing csv into a given xml structure is almost cherry picked for xslt and xpath wheras linq2XMl will give you problems if you have an xml document that has optional elements. so i tend to use xpath to really lock down the xml format so that it is explicit and to keep my linq2xml mapping very very simple.
The result is a lot less bugs and much faster development.
no idea why the guy is talking about linq2xsd ... its a discontinued project that has very very little documentation. stay away from it.
Xdocument is an object that is actually enjoyable to work with ... xmldocument is one that is just fiddly imo. Obviously it depends on the task at hand, but the lack of xpath 2.0 makes me tend to use it as a data cleanser and then let linq2XMl do the real work.
as far as search goes, you can do everything that linq2xml does in xpath, the thing is that syntactically i far prefer to use linq2sql and play with strongly typed collections than mess about with xpath. Its much easier to come back to at a later date and adapt. Also you dont have to worry about syntax differences between xpath implementations and especially with regex implementations
任何一个。这取决于。
取决于您(和您的团队)的知识(即 XPath 不会有效,因为没有人知道 XPath,但所有人都知道 LINQ to XML)。此外,某些操作在其中一种或另一种中可能会更容易。
Either. It depends.
Depending on your (and your team's) knowledge (i.e. XPath will not be effective is no one knows XPath, but all know LINQ to XML). Also some operations can be easier in one or the other.
您需要首先定义标准来判断什么是最好的。您需要决定是将 XPath 1.0 还是 2.0 与 LINQ to XML 进行比较。 Microsoft 不支持 XPath 2.0,但存在第三方解决方案,例如 Saxon 9 或 XQSharp。
You will need to define criteria first to judge what is best. And you need to decide whether you want to compare XPath 1.0 or 2.0 with LINQ to XML. Microsoft does not support XPath 2.0 but third party solutions exist, like Saxon 9 or like XQSharp.