.net 如何在 XElement 或 LINQ 中使用 XPath?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3642829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:42:02  来源:igfitidea点击:

How to use XPath with XElement or LINQ?

.netxmlxpathlinq-to-xmlxelement

提问by Paul Fryer

Consider the following XML:

考虑以下 XML:

<response>
  <status_code>200</status_code>
  <status_txt>OK</status_txt>
  <data>
    <url>http://bit.ly/b47LVi</url>
    <hash>b47LVi</hash>
    <global_hash>9EJa3m</global_hash>
    <long_url>http://www.tumblr.com/docs/en/api#api_write</long_url>
    <new_hash>0</new_hash>
  </data>
</response>

I'm looking for a really short way to get just the value of the <hash>element. I tried:

我正在寻找一种非常简单的方法来获取<hash>元素的值。我试过:

var hash = xml.Element("hash").Value;

But that's not working. Is it possible to provide an XPath query to an XElement? I can do it with the older System.Xmlframework, doing something like:

但这行不通。是否可以向XElement? 我可以用旧的System.Xml框架来做,做类似的事情:

xml.Node("/response/data/hash").Value

Is there something like this in a LINQ namespace?

LINQ 命名空间中有这样的东西吗?



UPDATE:

更新:

After monkeying around with this some more I found a way to do what I'm trying to do:

在反复琢磨这个之后,我找到了一种方法来做我想做的事情:

var hash = xml.Descendants("hash").FirstOrDefault().Value;

I'd still be interested to see if anyone has a better solution?

我仍然有兴趣看看是否有人有更好的解决方案?

回答by Richard

To use XPath with LINQ to XML add a using declaration for System.Xml.XPath, this will bring the extension methods of System.Xml.XPath.Extensionsinto scope.

要将 XPath 与 LINQ to XML 一起使用System.Xml.XPath,请添加一个 using 声明 for ,这会将 的扩展方法System.Xml.XPath.Extensions引入范围。

In your example:

在你的例子中:

var value = (string)xml.XPathEvaluate("/response/data/hash");

回答by Jon Skeet

Others have entirely reasonably suggested how to use "native" LINQ to XML queries to do what you want.

其他人完全合理​​地建议了如何使用“本机”LINQ to XML 查询来执行您想要的操作。

However, in the interests of providing lots of alternatives, consider XPathSelectElement, XPathSelectElementsand XPathEvaluateto evaluate XPath expressions against an XNode(they're all extension methods on XNode). You can also use CreateNavigatorto create an XPathNavigatorfor an XNode.

但是,为了提供大量替代方案,请考虑XPathSelectElementXPathSelectElementsXPathEvaluate根据 an 评估 XPath 表达式XNode(它们都是 上的扩展方法XNode)。您还可以使用CreateNavigator创建XPathNavigator一个XNode

Personally I'm a big fan of using the LINQ to XML API directly, as I'm a big LINQ fan, but if you're more comfortable with XPath, the above may help you.

我个人是直接使用 LINQ to XML API 的忠实粉丝,因为我是 LINQ 的忠实粉丝,但如果您更熟悉 XPath,以上内容可能会对您有所帮助。

回答by abhishek

See, when dealing with LINQ to XML why dont you use LINQ to get the actual object.

看,在处理 LINQ to XML 时为什么不使用 LINQ 来获取实际对象。

Descendants find each element from the whole XML and lists all the objects that matches the name specified. So in your case hash is the name which it finds.

后代从整个 XML 中找到每个元素并列出与指定名称匹配的所有对象。因此,在您的情况下,哈希是它找到的名称。

So, rather than doing

所以,而不是做

var hash = xml.Descendants("hash").FirstOrDefault().Value;

I would break apart like :

我会像这样分开:

var elements = xml.Descendants("hash");
var hash = elements.FirstOrDefault();

if(hash != null)
 hash.Value // as hash can be null when default. 

In this way you might also get attributes, nodes elements etc.

通过这种方式,您还可以获得属性、节点元素等。

Check this article to get clear idea about it so that it helps. http://www.codeproject.com/KB/linq/LINQtoXML.aspxI hope this will help you.

检查这篇文章以清楚地了解它,以便它有所帮助。 http://www.codeproject.com/KB/linq/LINQtoXML.aspx我希望这会帮助你。

回答by panpawel

You can use .Element() method to chain the elements to form XPath-like structure.

您可以使用 .Element() 方法链接元素以形成类似 XPath 的结构。

For your example:

对于您的示例:

XElement xml = XElement.Parse(@"...your xml...");
XElement hash = xml.Element("data").Element("hash");

回答by TGH

I have tried to come up with a LINQesq framework for generating xpath. It lets you describe xpath using c# lambda expressions

我试图提出一个用于生成 xpath 的 LINQesq 框架。它允许您使用 c# lambda 表达式描述 xpath

var xpath = CreateXpath.Where(e => e.TargetElementName == "td" && e.Parent.Name == "tr");

var xpath = CreateXpath.Where(e => e.TargetElementName == "td").Select(e => e.Text);

Not sure if this is helpful in this context, but you can find documentation here:

不确定这在这种情况下是否有帮助,但您可以在此处找到文档:

http://www.syntaxsuccess.com/viewarticle/how-to-create-xpath-using-linq

http://www.syntaxsuccess.com/viewarticle/how-to-create-xpath-using-linq