使用 class 和 Html Agility Pack 在跨度后获取文本

Get text after the span with a class with Html Agility Pack

我有这个结构

   <span class="glyphicon glyphicon-user"></span>
                         123                       <br>

我想得到“123”

此代码在标签之间获取(如果存在)

var username = document.DocumentNode.SelectSingleNode("//p[@class='glyphicon-user']");
//Response.Write(username.InnerText);

但我想得到“123”

这是一种可能的方式,完全基于目前发布的 HTML sinppet:

var query = "//span[@class='glyphicon glyphicon-user']/following-sibling::text()[1]";
var username = document.DocumentNode.SelectSingleNode(query);
Console.WriteLine(username.InnerText.Trim());

基本上,XPath 会查找具有特定 class 属性值的 span,然后 return 最近的文本节点 位于 span。根据您的具体需要和实际的 HTML 结构,可能会有更好的方法。

dotnetfiddle demo

输出:

123