Find Node based on InnerText

Topics: Developer Forum
Jul 30, 2007 at 4:56 PM
Hello,

I'm wondering what steps I need to take to find a node by searching for a text in it's innerText?

Thanks.
Eclipse
Aug 1, 2007 at 9:30 PM
Edited Aug 1, 2007 at 9:30 PM
Below is what I did to accomplish the task in VB. The Code only returns the first occurence of the string, but can be easily modified to return a collection.

Public Function GetNodeByInnerText(ByVal sHTML As String, ByVal sText As String) As HtmlNode
Dim doc As New HtmlAgilityPack.HtmlDocument
doc.LoadHtml(sHtml)
Dim Position As Integer = sHtml.ToString.IndexOf(sText)
Return FindNodeByPosition(doc.DocumentNode, Position)
End Function

Private Function FindNodeByPosition(ByVal node As HtmlNode, ByVal Position As Integer) As HtmlNode
Dim foundNode As HtmlNode = Nothing
For Each child As HtmlNode In node.ChildNodes
If child.StreamPosition <= Position And (IsNothing(child.NextSibling) OrElse child.NextSibling.StreamPosition > Position) Then
If child.ChildNodes.Count > 0 And Not (child.ChildNodes.Count = 1 AndAlso child.InnerHtml = child.FirstChild.InnerHtml) Then
foundNode = FindNodeByPosition(child, Position)
Exit For
Else
Return child
End If
End If
Next
Return foundNode
End Function


eclipse wrote:
Hello,

I'm wondering what steps I need to take to find a node by searching for a text in it's innerText?

Thanks.
Eclipse

Aug 7, 2007 at 9:23 PM
I replaced doing this with using XPath query instead, which is more accurate than searching text. I added another post where I created a new property to the Html Node that returns the node's XPath http://www.codeplex.com/htmlagilitypack/Thread/View.aspx?ThreadId=13527