codeplex 페이지에 문서가없는 것 같고 어떤 이유로 intellisense에서 htmlagilitypack에 대한 사용 가능한 메소드 나 아무것도 표시하지 않습니다 (예 : MyHtmlDocument.DocumentNode를 입력 할 때). - 내가 할 수있는 일에 대해 알려주는 인텔리 센스가 없습니다. 다음 것)
모든 <a> 태그와 그 내용을 HTML 문서 본문에서 제거하는 방법을 알아야합니다.이 태그는 A 태그의 내용을 여전히 반환하기 때문에 Body에서 Node.InnerText를 사용할 수 없습니다.
다음은 HTML 예제입니다.
<html>
<body>
I was born in <a name=BC>Toronto</a> and now I live in barrie
</body>
</html>
나는 돌아올 필요가있어.
I was born in and now I live in barrie
고마워, 도움을 주셔서 감사합니다!
도마
이것은 당신에게 당신이 요구하는 결과를 가져옵니다. 재귀 적 방법을 사용하여 모든 HTML 노드를 드릴 다운하고 새로운 if 구문을 추가하여 더 많은 노드를 제거 할 수 있습니다.
Public Sub Test()
Dim document = New HtmlDocument() With { _
Key .OptionOutputAsXml = True _
}
document.LoadHtml("<html><body>I was born in <a name=BC>Toronto</a> and now I live in barrie</body></html>")
For i As var = 0 To document.DocumentNode.ChildNodes.Count - 1
RecursiveMethod(document.DocumentNode.ChildNodes(i))
Next
Console.Out.WriteLine(document.DocumentNode.InnerHtml.Replace(" ", " "))
End Sub
Public Sub RecursiveMethod(child As HtmlNode)
For x As var = 0 To child.ChildNodes.Count - 1
Dim node = child.ChildNodes(x)
If node.Name = "a" Then
node.RemoveAll() //removes all the child nodes of "a"
node.Remove() //removes the actual "a" node
Else
If node.HasChildNodes Then
RecursiveMethod(node)
End If
End If
Next
End Sub
줄을 따라 뭔가가 (미안 해요 내 코드는 C #하지만 그럼에도 불구하고 도움이되기를 바랍니다)
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("some html markup here");
HtmlNodeCollection links = doc.DocumentNode.SelectNodes("//a[@name]");
foreach(HtmlNode link in links)
{
link.Remove();
}
//then one of the many doc.Save(...) overrides to actually get the result of the operation.