Hey everyone,
What's i'm trying to accomplish is this - I have a webbrowser control & a treeview control...once I load a website I create a HTML DOM tree representation of the website using the code below...
What I want to do now, is the following:
- mouse over any HTML element, which will then select the corresponding tree node on the HTML DOM Tree
- select any tree node and have the corresponding HTML element highlight, with a red border around it
Does anyone have any pointers / suggestions - I would really appreciate it...
Private Sub UpdateDOM(ByVal HTMLDocument As mshtml.IHTMLDocument3)
TreeView1.Nodes.Clear()
TreeView1.BeginUpdate()
Dim domRoot As mshtml.IHTMLElement = HTMLDocument.documentElement
Dim treeRoot As New TreeNode()
treeRoot.Text = "<html>"
treeRoot.Tag = domRoot
TreeView1.Nodes.Add(treeRoot)
CopyElementToTree(domRoot, treeRoot)
TreeView1.EndUpdate()
TreeView1.ExpandAll()
End Sub
Private Shared Sub CopyElementToTree(ByVal element As mshtml.IHTMLElement, ByVal treeNode As TreeNode)
Dim children As mshtml.IHTMLElementCollection = CType(element.children, mshtml.IHTMLElementCollection)
If children.length <> 0 Then
For Each e As mshtml.IHTMLElement In children
Dim treeChild As New TreeNode()
treeChild.Text = getAttrs(e)
treeChild.Tag = e
treeNode.Nodes.Add(treeChild)
CopyElementToTree(e, treeChild)
Next e
Else
If Not String.IsNullOrEmpty(element.innerText) Then
Dim textNode As New TreeNode()
textNode.Text = String.Format("""{0}""", element.innerHTML)
textNode.Tag = element
treeNode.Nodes.Add(textNode)
End If
End If
End Sub
Private Shared Function getAttrs(ByVal element As mshtml.IHTMLElement) As String
Dim result As New StringBuilder("<")
result.Append(element.tagName.ToLower())
'result.Append(element.innerHTML);
Dim domnode As mshtml.IHTMLDOMNode = CType(element, mshtml.IHTMLDOMNode)
Dim attrs As mshtml.IHTMLAttributeCollection = CType(domnode.attributes, mshtml.IHTMLAttributeCollection)
If attrs IsNot Nothing Then
For Each attr As mshtml.IHTMLDOMAttribute In attrs
If Not attr.specified Then
Continue For
End If
result.Append(" ")
Dim nodeValue As String = String.Empty
If attr.nodeValue IsNot Nothing Then
nodeValue = attr.nodeValue.ToString()
End If
result.Append(String.Format("{0}=""{1}""", attr.nodeName, nodeValue))
Next attr
End If
result.Append(">")
Return result.ToString()
End Function