주어진 클래스가있는 모든 요소를 선택하고 HTML 문자열에서 제거하려고합니다.Agility Pack을 사용하여 클래스가 지정된 HTML에서 모든 요소 제거
이것은 소스가 분명히 클래스 이름을 가진 4 개의 요소를 보여 주지만 지금까지 내가 제거한 것 같지 않습니다.
// Filter page HTML to display required content
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
// filePath is a path to a file containing the html
htmlDoc.LoadHtml(pageHTML);
// ParseErrors is an ArrayList containing any errors from the Load statement);
if (!htmlDoc.ParseErrors.Any())
{
// Remove all elements marked with pdf-ignore class
HtmlNodeCollection nodes = htmlDoc.DocumentNode.SelectNodes("//body[@class='pdf-ignore']");
// Remove the collection from above
foreach (var node in nodes)
{
node.Remove();
}
}
편집 : 문서를 구문 분석하고 SelectNodes 라인은 단지 아무것도 반환하지 명중되고 그냥 명확하게.
이<input type=\"submit\" name=\"ctl00$MainContent$PrintBtn\" value=\"Print Shotlist\" onclick=\"window.print();\" id=\"MainContent_PrintBtn\" class=\"pdf-ignore\">
나는 실제로 이것을 알아 차 렸습니다. 질문을 업데이트했습니다. 그것은 비록 어떤 노드를 반환하지 않습니다. 내 요소는 문서 내의 여러 위치에 있으며 모두 제거하려고합니다. 나는 @를 생략하려고 시도했지만 아무 것도 고르지 않습니다. – Andrew
@Andi, HTML 문자열을 게시 할 수 있습니까? 또한 업데이트 된 질문에 컬렉션에 적어도 하나의 요소가 있다면'true' 인'htmlDoc.ParseErrors.Any()'조건을 사용합니다 (여기에 적어도 하나의 오류가 있습니다). – Alex
도움을 주셔서 감사합니다. – Andrew