views:

854

answers:

2

I asked the question in a codeplex discussion but I hope to get a quicker answer here at stackoverflow.

So, I use HTML Agility Pack for HTML parsing in C#. I have the following 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>

And I need to get all p elements with class "paragraph" that exist after the p element with class "specific".

Is there a way to do that?

Thanks.

+1  A: 

Try this

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);
   }
}
Mark Dickinson
I guess you just looked over the question, not actually read it. :) I parse the HTML in C# with HTML Agility Pack, and I need to select only the p tags with class="paragraph" that are after the p tag with class="specific".
morsanu
Sorry about that, hope this answer is more useful (you'll need a reference to System.Linq). :)
Mark Dickinson
That will select ALL the p tags with "paragraph" class. I need only the ones AFTER the p tag with class="specific".
morsanu
Sorry it is early here, hope that helps, it's hard to think of a more elegant way.
Mark Dickinson
No problem, mate, I appreciate you're trying to help. This solution crossed my mind but I am still hoping that I don't have to use it. Maybe a more elegant one will pop up. If not, I will mark you answer.
morsanu
+3  A: 

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

Use SkipWhile

e.g. in LINQPad you get 5,6,7 from:

int[] a = { 6, 5, 6 ,7 };
a.SkipWhile(x=>x!=6).Skip(1).Dump();

So depending 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, ugly version)

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

(or in some cases - not if your expression is already filtering appropriately)

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

EDIT: I'd probably create an extension method:

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

Anyone care to search up prior art for this? Any good name suggestions?

Ruben Bartelink
SkipWhile is cool +1
Mark Dickinson
This is exactly what I needed. Thanks.
morsanu
I'll be using it myself soon too, so thanks for asking!
Ruben Bartelink