如何在XElement或LINQ中使用XPath ?

时间:2022-11-11 23:49:08

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:

我在寻找一种非常简单的方法来获取 元素的值。我试着:

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.Xml framework, doing something like:

但这不是工作。是否可以向XElement提供XPath查询?我可以用旧的系统来做。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?

我还是很想看看有没有更好的办法?

5 个解决方案

#1


123  

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.Extensions into scope.

要使用LINQ到XML的XPath,添加System.Xml的使用声明。XPath将引入System.Xml.XPath的扩展方法。扩展到范围。

In your example:

在你的例子:

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

#2


33  

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

其他人完全合理地建议如何在XML查询中使用“本机”LINQ来执行所需的操作。

However, in the interests of providing lots of alternatives, consider XPathSelectElement, XPathSelectElements and XPathEvaluate to evaluate XPath expressions against an XNode (they're all extension methods on XNode). You can also use CreateNavigator to create an XPathNavigator for an XNode.

但是,为了提供许多替代方法,考虑XPathSelectElement、XPathSelectElements和XPathEvaluate对XNode的XPath表达式进行计算(它们都是XNode的扩展方法)。您还可以使用CreateNavigator为XNode创建一个XPathNavigator。

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到XML API,因为我是LINQ的忠实粉丝,但是如果您更熟悉XPath,上面的内容可能会对您有所帮助。

#3


13  

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

在处理LINQ到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.aspx I hope this will help you.

检查这篇文章,了解它,以便它有帮助。我希望这能对你有所帮助。

#4


6  

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");

#5


1  

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

我已经尝试使用LINQesq框架来生成xpath。它允许您使用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

#1


123  

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.Extensions into scope.

要使用LINQ到XML的XPath,添加System.Xml的使用声明。XPath将引入System.Xml.XPath的扩展方法。扩展到范围。

In your example:

在你的例子:

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

#2


33  

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

其他人完全合理地建议如何在XML查询中使用“本机”LINQ来执行所需的操作。

However, in the interests of providing lots of alternatives, consider XPathSelectElement, XPathSelectElements and XPathEvaluate to evaluate XPath expressions against an XNode (they're all extension methods on XNode). You can also use CreateNavigator to create an XPathNavigator for an XNode.

但是,为了提供许多替代方法,考虑XPathSelectElement、XPathSelectElements和XPathEvaluate对XNode的XPath表达式进行计算(它们都是XNode的扩展方法)。您还可以使用CreateNavigator为XNode创建一个XPathNavigator。

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到XML API,因为我是LINQ的忠实粉丝,但是如果您更熟悉XPath,上面的内容可能会对您有所帮助。

#3


13  

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

在处理LINQ到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.aspx I hope this will help you.

检查这篇文章,了解它,以便它有帮助。我希望这能对你有所帮助。

#4


6  

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");

#5


1  

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

我已经尝试使用LINQesq框架来生成xpath。它允许您使用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