简体   繁体   English

什么是更好的XPath或Linq2Xml

[英]What is better XPath or Linq2Xml

What is the "best" way to search in xml? 在xml中搜索的“最佳”方式是什么?
Xpath or Linq2xml. Xpath或Linq2xml。

I'm asking this because we need to do a lot of searching in xml. 我问这个是因为我们需要在xml中进行大量搜索。
I'm always using XPath (since I've been using this from .NET 1.1). 我总是使用XPath(因为我一直在使用.NET 1.1)。 But with the introduction of Linq you can easely use Linq2Sql. 但随着Linq的推出,你可以轻松使用Linq2Sql。

Regards, M. 问候,M。

i use both extensively also xsl 我也广泛使用xsl

they have very different uses imo 他们有非常不同的用途imo

xpath is great for manipulating xml documents wheras linq2xml is great for mapping them into object collections. xpath非常适合处理xml文档,而linq2xml非常适合将它们映射到对象集合中。

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. 例如,将csv解析为给定的xml结构几乎可以用于xslt和xpath,如果你有一个包含可选元素的xml文档,linq2XMl会给你带来问题。 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. 所以我倾向于使用xpath来真正锁定xml格式,以便它是显式的并保持我的linq2xml映射非常简单。

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. 不知道为什么这个人正在谈论linq2xsd ......这是一个已停产的项目,文档非常少。 stay away from it. 远离它。

Xdocument is an object that is actually enjoyable to work with ... xmldocument is one that is just fiddly imo. Xdocument是一个实际上可以使用的对象... xmldocument是一个非常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. 显然它取决于手头的任务,但缺少xpath 2.0使我倾向于将它用作数据清理器,然后让linq2XMl完成真正的工作。

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. 就搜索而言,你可以完成linq2xml在xpath中所做的一切,从语法上来说,我更喜欢使用linq2sql并使用强类型集合而不是使用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实现之间的语法差异,尤其是正则表达式实现

Either. 无论是。 It depends. 这取决于。

Depending on your (and your team's) knowledge (ie XPath will not be effective is no one knows XPath, but all know LINQ to XML). 取决于你(和你的团队)的知识(即XPath不会有效,没有人知道XPath,但都知道LINQ to XML)。 Also some operations can be easier in one or the other. 此外,一些操作在一个或另一个中可以更容易。

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. 您需要决定是否要将XPath 1.0或2.0与LINQ to XML进行比较。 Microsoft does not support XPath 2.0 but third party solutions exist, like Saxon 9 or like XQSharp . Microsoft不支持XPath 2.0,但存在第三方解决方案,如Saxon 9XQSharp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM