Select nodes following a given node in HTML Agility Pack c# html html-agility-pack


I hoped to obtain a speedier response by asking the question here on stackoverflow, in a conversation on the codeplex.

I thus use HTML Agility Pack to parse HTML in C#. I've got the next html structure:

   <p class="paragraph">text</p>
   <p class="paragraph">text</p>
   <p class="specific">text</p>
   <p class="paragraph">text</p>
   <p class="paragraph">text</p>

After the p element with class "particular," I also need to get all subsequent p elements with class "paragraph".

Is it possible to accomplish?


7/4/2010 9:49:02 AM

Accepted Answer

utilizing.Class as in Mark's illustration (if that doesnt exist, substitute whatever is appropriate)

use zzz-9 zzz

For instance, you receive5,6,7 from:

int[] a = { 6, 5, 6 ,7 };

As a result, based on the type SelectNodes returns, either:

.SelectNodes( "/p" ).SkipWhile( p => p.Class != "specific" ).Skip(1)


.SelectNodes( "/p" ).Cast<XX>().SkipWhile( p => p.Class != "specific" ).Skip(1)

(Or, a crude variant)

.SelectNodes( "/p" ).SkipWhile( p => ((XX)p).Class != "specific" ).Skip(1)

(or sometimes not, if your expression is already properly filtering)

.SelectNodes( "/p" ).OfType<XX>().SkipWhile( p => p.Class != "specific" ).Skip(1)

EDIT: I would most likely develop an extension method:

static class HapExtensions
    public IEnumerable<T> SkipUntilAfter( this IEnumerable<T> sequence, Predicate<T> predicate) {
        return sequence.SkipWhile( predicate).Skip(1);

Anyone willing to look up the relevant earlier art? Any thoughts for a nice name?

12/14/2009 11:52:45 AM

Popular Answer

Try it.

bool latterDayParagraphs = false;
List<DocumentNode> nodes = new List<DocumentNode>();
foreach(var pElement in doc.DocumentNode.SelectNodes("/p"))
   if(pElement.Class != "paragraph") 
      latterDayParagraphs = true;

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow