English 中文(简体)
HTML Agility Pack - Select nodes after specific node
原标题:

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.

最佳回答

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?

问题回答

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);
   }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签