最終的に、HtmlAgilityPack.HtmlDocument.SelectNodes()を使用してノードを選択し、選択中のノードが除外タグであるかどうか、およびそのような親があるかどうかを(再帰的に)チェックしました。
Public Const cAlphabet As String = "AÁÄBCČDĎEÉĚFGHIÍJKLĹĽMNŇOÓÔPQRŔŘSŠTŤUÚŮVWXYÝZŽ0123456789" ' Accepted chars '
Dim nodes As HtmlNodeCollection = nothing
Dim doc As HtmlDocument = New HtmlDocument()
' div encapsulation is used for text which is not between any tags. '
' iHtmlText is variable which holds html document in text form '
doc.LoadHtml(String.Format("<div>{0}</div>", If(iHtmlText, String.Empty)))
' "FIND_THIS_TEXT" can be any text which you want to find '
' Node selecting is case insensitive due to translate feature of xpath '
nodes = doc.DocumentNode.SelectNodes(
String.Format("//*[contains(translate(text(), '{0}', '{1}'), '{2}')]",
cAlphabet, cAlphabet.ToLower, "FIND_THIS_TEXT".ToLower))
For Each node As HtmlNode In nodes
If (IsNotOrNestedInSpecifiedNode(node, "a", "h1", "h2", "h3", "h4", "h5", "h6")) Then
' do something with the node here '
End If
Next
上記のコードで使用されるIsNotOrNestedInSpecifiedNode関数:
Private Function IsNotOrNestedInSpecifiedNode(ByVal iNode As HtmlNode, ByVal ParamArray iExcludedHtmlTags() As String) As Boolean
Dim ret As Boolean = False
If (iNode.Name.IsIn(iExcludedHtmlTags)) Then
ret = False
ElseIf (iNode.ParentNode IsNot Nothing) Then
ret = IsNotOrNestedInSpecifiedNode(iNode.ParentNode, iExcludedHtmlTags)
Else
ret = True
End If
Return ret
End Function