c# htmlagility Select 具体节点取决于内部文本

c# htmlagility Select specifics nodes depending on the innertext

我在 div 中有多个标签。这些标签有一个 class 名称 "Return" 或 "Going" 像:

<div>
    <label class="going"></label>
    <label class="going"></label>
    <label class="going"></label>
    <label class="going"></label>
    <label class="return"></label>
    <label class="return"></label>
    <label class="return"></label>
</div>

我想获得两个 IEnumerable 标签,一个 going 和一个 return; 像

var going = node.SelectNodes("label").Where(item => item.InnerHtml == "going");
var return= node.SelectNodes("label").Where(item => item.InnerHtml == "return");

您可能想使用此方法:

public static bool HasClass(this HtmlNode node, params string[] classValueArray)
{
    var classValue = node.GetAttributeValue("class", "");
    var classValues = classValue.Split(' ');
    return classValueArray.All(c => classValues.Contains(c));
}
//use it like this
doc.DocumentNode.Descendants("label").Where(_ => _.HasClass("going"))