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