I have the following code to find some elements in my HTML and do something with those which have a certain attribute:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlString);
foreach (HtmlNode div in doc.DocumentNode.SelectNodes("//div[contains(@class,'item')]"))
{
var hasAttachments = div.Attributes.FirstOrDefault(a => a.Name.Equals("hasattachments"));
if (hasAttachments.Value.Equals("True"))
{
var itemId = div.Attributes.FirstOrDefault(a => a.Name.Equals("itemid")).Value;
doStuffWithItemId(itemId);
}
}
I was wondering if I could merge my query to find elements which posses the class and the value of an attribute set to "True"
, something like this:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlString);
foreach (HtmlNode div in doc.DocumentNode.SelectNodes("//div[contains(@class,'item')] and //div[@data-hasattachments=\"True\"]"))
{
var itemId = div.Attributes.FirstOrDefault(a => a.Name.Equals("itemid")).Value;
doStuffWithItemId(itemId);
}