HTML 민첩성 팩 - 특정 노드 후 노드를 선택하십시오
-
19-09-2019 - |
문제
나는 질문을했다 코드 플렉스 토론에서 그러나 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 사용 (존재하지 않는 경우 적절한 것을 대체하십시오)
사용 건너 뛰기
예를 들어 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);
}
}
제휴하지 않습니다 StackOverflow