Я задал вопрос в codeplex обсуждении, но я надеюсь получить более быстрый ответ здесь в stackoverflow.
Так, я использую Пакет Гибкости HTML для парсинга HTML в C#. У меня есть следующая структура HTML:
text
text
text
text
text
И я должен получить все p элементы с классом "абзац", которые существуют после p элемента с классом "конкретный".
Существует ли способ сделать это?
Спасибо.
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?
Попробуйте это
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);
}
}