Html Agility Pack - - Retrieve Node with an "empty" class Attribute OR Select PAIR nodes (one and the next node below)

I need to extract pairs of nodes from html code (either HtmlDocument or List of HtmlNodes).

The problem is that I need to select a node (from a list or HtmlDocument, it doesn’t matter what I choose the best solution), which has a class attribute, but no value at all (see the pictures).

Another (better solution, I think) would be to select "node and its immediate next-sibling :: li [1] (link # 2) and its probably what I will do for this part of my program. Link # 2, it seems helps a little, but I don’t know how to use it in such a way as "get all nodes and their first next".

Id like 2 things: - A bit of code to get "one node by class And its first next node", I did not use XPathes (or w / e its callable), but I did not use it - If possible, a way to get "node that has a class attribute but NO VALUE "

The thing is, I have to choose an HtmlNode with the "zero gravity" class later, and Im looking for a way to do this. The idea (if not yet clear enough) would be something like this:

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 the following 2 nodes in HTML + HTMLAgilitypack But I have never used this before (I could use this to select "node and its immediate next (not going to use this, too creepy)

:

, 2 :

Update

,

<li class>

.

, ( , , , ^^, ), "" , node . : <li class="A">...</> = > <li class="B">...</> = > <li class="A">...</> = > <li class="B">...</> = >

, A/class B ( , a#, A Content, , B).

tl; dr: - 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  ( <li class="B">) );

pairs.add(pair);


}

return pairs;
}
+4
1

TBH, , .

, " node node", XPathes ( w/e ), -

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;

, "node, NO VALUE"

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

Source: https://habr.com/ru/post/1676148/


All Articles