Select nodes following a given node in HTML Agility Pack

asp.net c# html html-agility-pack

Question

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:

<body>
   <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>
</body>

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

Is it possible to accomplish?

Thanks.

1
8
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 };
a.SkipWhile(x=>x!=6).Skip(1).Dump();

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

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

or

.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?

6
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;
      continue;
   }
   if(latterDayParagraphs)
   {
      nodes.Add(pElement);
   }
}


Related Questions





Related

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