나는 코드 플렉스 토론에서 질문 을 했지만 stackoverflow에서 더 빠른 대답을 얻길 바랍니다.
그래서 C #에서 HTML 구문 분석을 위해 HTML 민첩성 팩을 사용합니다. 나는 다음과 같은 HTML 구조를 가지고있다 :
<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>
그리고 클래스 "특정"클래스의 p 요소 다음에 존재하는 클래스 "단락"이있는 모든 p 요소를 가져와야합니다.
그렇게 할 수있는 방법이 있습니까?
감사.
Mark의 예와 같이 .Class를 사용하십시오 (해당하지 않는 경우 적절한 것으로 대체하십시오)
SkipWhile 사용
예 : LINQPad 에서 5,6,7
을 얻습니다.
int[] a = { 6, 5, 6 ,7 };
a.SkipWhile(x=>x!=6).Skip(1).Dump();
SelectNodes 유형에 따라 다음 중 하나가 반환됩니다.
.SelectNodes( "/p" ).SkipWhile( p => p.Class != "specific" ).Skip(1)
또는
.SelectNodes( "/p" ).Cast<XX>().SkipWhile( p => p.Class != "specific" ).Skip(1)
(또는 추한 버전)
.SelectNodes( "/p" ).SkipWhile( p => ((XX)p).Class != "specific" ).Skip(1)
(또는 어떤 경우에는 - 귀하의 표현이 이미 적절하게 필터링되고 있지 않은 경우)
.SelectNodes( "/p" ).OfType<XX>().SkipWhile( p => p.Class != "specific" ).Skip(1)
편집 : 아마도 확장 메서드를 만들 것이라고 :
static class HapExtensions
{
public IEnumerable<T> SkipUntilAfter( this IEnumerable<T> sequence, Predicate<T> predicate) {
return sequence.SkipWhile( predicate).Skip(1);
}
}
이것을 위해 선행 기술을 검색하는 사람은 누구입니까? 좋은 이름 제안?
이 시도
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);
}
}