Html Agility Pack - – Extract Node with “empty” class Attribute OR Select a PAIR of nodes (one and its immediate following node)

c#, html-agility-pack

Solution

TBH, I am not quite understand your question clearly but here is my attempt to answer it.

A bit of code to get “one node by class AND its first following node”, I haven’t used XPathes (or w/e it’s called) yet so I’m not used to -

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));
}
doc.DocumentNode.Descendants("li").FirstOrDefault(_ => _.HasClass("classname")).NextSibling;

If it’s possible, a way to get a “node which has the class Attribute but NO VALUE”

doc.DocumentNode.Descendants("li").Where(_ => string.IsNullOrEmpty(_.GetAttributeValue("class", "")))

Problem

I need to extract pairs of nodes from a html code (either HtmlDocument or a List of HtmlNodes). The problem is I need to select a node (from a List or a HtmlDocument, doesn’t matter I’ll pick the best solution) that has a class Attribute but no value at all (see pictures). Another (better solution, I think) would be to select the “” node and its immediate following-sibling::li[1] (link #2) and it’s probably what I will do for this part of my program. Link #2 seems to help a bit but I don’t know how to use it in a way like “get all nodes AND its first following-sibling”. I’d like 2 things: - A bit of code to get “one node by class AND its first following node”, I haven’t used XPathes (or w/e it’s called) yet so I’m not used to - If it’s possible, a way to get a “node which has the class Attribute but NO VALUE” The thing is, I’ll have to select HtmlNode with “valueless“ class later and I’m looking for a way to do it. The idea (if it’s still not clear enough) would be something like: ``` var r = htmlDoc.DocumentNode.Descendants("li").Where(d => d.Attributes["class"].Value.Equals(NULL)); //I’m not sure about the [enter image description here][1].Value.Equals() ^^’ ``` Links: - How to get next 2 nodes in HTML + HTMLAgilitypack But I never used this before (I could use this to select the “” node and its immediate following (Not going to use this, way too spooky) - select an element next to current element HtmlAgilityPack I was planning on using this BUT I never used xpath for htmldoc yet (looking into it now) Pictures: sample code of what I'd like to extract Trying to add 2 pictures : enter image description here Update Okay so, with Hung Cao's help I was able to select the ``` <li class> ``` nodes. Now, what I would like to do (it's what I first explained but it wasn't very clear, even for me ^^ so I'll try to use a specific example), is to select "pairs" of nodes, more precisely one specific node and its immediate first sibling. I have : ` <li class="A">...</>` => first pair` <li class="B">...</>` => first pair` <li class="A">...</>` => second pair` <li class="B">...</>`=> second pair And I would like to end up with like a Collection or an Array containing pairs of class A/class B (Actually, I'm using a c# class which is basically A's Content, and in it there is an array in which I store B class's elements). tl;dr: I'd like something along the lines of ` public List<Pair> ExtractPairs(HtmlAgilityPack.HtmlDocument htmlDoc){` ``` List<Pair> pairs = new List<>(); foreach (HtmlNode node in htmlDoc.DocumentNode.SelectNodes("//li[@class='A']")) { ``` `Pair pair = new Pair(node,node` 's first-sibling here (which will always be a `<li class="B">`) `);` ``` pairs.add(pair); } return pairs; } ```

Original source