Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XPath选择节点_C#_.net_Html Agility Pack - Fatal编程技术网

C# XPath选择节点

C# XPath选择节点,c#,.net,html-agility-pack,C#,.net,Html Agility Pack,我有: 我如何使用HtmlAgilityPack在href中仅选择包含“id”的项目 输出: <div id="foo"> <a href="/xxx.php"> xx </a> <a href="/xy.php"> xy </a> <a href="/uid.php?id=123"> 123 </a> <a href="/uid.php?id=344"> 344 </a> </

我有:


我如何使用HtmlAgilityPack在href中仅选择包含“id”的项目

输出:

<div id="foo">
<a href="/xxx.php"> xx </a>
<a href="/xy.php"> xy </a>
<a href="/uid.php?id=123"> 123 </a>
<a href="/uid.php?id=344"> 344 </a>
</div>


谢谢,高级。

下面的xpath表达式应该选择包含文本“id”的
href
标记的所有
a
元素

我可以使用以下代码在href属性中选择id为的a标记:

var xpathExpression = "//a[contains(@href, 'id')]";
var htmlDoc=new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(
@"
");
var aTags=htmlDoc.DocumentNode.SelectNodes(//a[contains(@href,'id')]);
foreach(变量aTag在aTags中)
控制台写入线(aTag.OuterHtml);

以下xpath表达式应选择所有
a
元素,这些元素具有包含文本“id”的
href
标记

我可以使用以下代码在href属性中选择id为的a标记:

var xpathExpression = "//a[contains(@href, 'id')]";
var htmlDoc=new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(
@"
");
var aTags=htmlDoc.DocumentNode.SelectNodes(//a[contains(@href,'id')]);
foreach(变量aTag在aTags中)
控制台写入线(aTag.OuterHtml);

稍微简单一点的语法:
//a[contains(@href,'id')]
稍微简单一点的语法:
//a[contains(@href,'id')]
var htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(
  @"<div id=""foo"">
    <a href=""/xxx.php""> xx </a>
    <a href=""/xy.php""> xy </a> 
    <a href=""/uid.php?id=123""> 123 </a>
    <a href=""/uid.php?id=344""> 344 </a>
</div>");
var aTags = htmlDoc.DocumentNode.SelectNodes("//a[contains(@href, 'id')]");
foreach(var aTag in aTags)
Console.WriteLine(aTag.OuterHtml);