c# - 如何在 C# 中计算某个类的 <span> 实例?

标签 c#

args.Content;

是动态的。

在此示例中,它具有此值并且代表脚本输入:

<div class="b-db">
<span class="b-db-ac b-db-ac-th" role="button"></span>
<span class="b-db-ac b-db-ac-th" role="button"></span>
<span class="b-db-ac b-db-ac-th" role="button"></span>
<span class="b-db-ac b-db-ac-th" role="button"></span>
<span class="b-db-ac" role="button"></span>
</div>

如何计算 class="b-db-ac b-db-ac-th"的 span 实例数量?

在此示例中,输出应为:4

最佳答案

当您使用 html 时,请使用真正的 html 解析器,例如 HtmlAgilityPack

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(DATA);

var count = doc.DocumentNode.SelectNodes("//span[@class='b-db-ac b-db-ac-th']")
               .Count();

您甚至可以使用纯 Linq

var count2 = doc.DocumentNode.Descendants("span")
                .Where(s => s.Attributes["class"]!=null && 
                            s.Attributes["class"].Value == "b-db-ac b-db-ac-th")
                .Count();

关于c# - 如何在 C# 中计算某个类的 <span> 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26241419/

相关文章:

c# - 在 WPF 中将 Image.Source 绑定(bind)到字符串?

c# - Xamarin:绑定(bind)返回协议(protocol)的 objective-c 协议(protocol)

c# - 为什么我的接口(interface)不实现公共(public)方法?

c# - Telerik Rad Combo Box 以及如何使其失去焦点

c# - 带有字符串参数的 MongoDb c# Driver typed Builder

javascript - 在C#中删除一个字符

c# - 在一个可靠的解决方案中集成用 Python 编写的测试和用 C# 编写的测试

c# - 使用 Umbraco.Core.Persistence 从带有枚举的模型创建数据库表

c# - 使 WPF TextBox 拉伸(stretch)到可用空间而不随 Text 增长

c# - 堆栈溢出异常——为什么?