Question Parsing HTML content into a TreeView

VBobCat

Well-known member
Joined
Sep 6, 2011
Messages
137
Location
S?o Paulo, Brazil
Programming Experience
3-5
Hello people,

I wrote this code in order to parse HTML elements within an HTML document into a treeview, so I can study its structure. But my iteration must have something wrong, for it doesn't recover all the elements. Could someone help me to fix it? Thank you very much!

    Public Sub ShowDocument(ByVal document As HtmlDocument)
        TreeView1.BeginUpdate()
        TreeView1.Nodes.Clear()
        Dim ActElem As HtmlElement, ActNode As TreeNode, ActStem As TreeNodeCollection
        ActElem = document.Body.Parent
        ActStem = TreeView1.Nodes
        Do
            If ActElem Is Nothing Then Exit Do
            ActNode = New TreeNode With {.Text = ActElem.TagName, .Tag = ActElem.OuterHtml}
            ActStem.Add(ActNode)
            If ActElem.Children.Count > 0 Then
                ActElem = ActElem.FirstChild
                ActStem = ActNode.Nodes
            Else
                Do While ActElem.NextSibling Is Nothing
                    If ActElem = document.Body.Parent Then Exit Do
                    ActElem = ActElem.Parent
                    ActStem = ActNode.Parent.Parent.Nodes
                Loop
                ActElem = ActElem.NextSibling
            End If
        Loop
        TreeView1.EndUpdate()
        Me.Show()
    End Sub

 
Maybe it's better you do this with a recursive method:
    Private Sub AddNode(element As HtmlElement, nodes As TreeNodeCollection)
        Dim node = nodes.Add(element.TagName)
        node.Tag = element.OuterHtml
        For Each child As HtmlElement In element.Children
            AddNode(child, node.Nodes)
        Next
    End Sub

example usage:
            With Me.TreeView1
                .Nodes.Clear()
                .BeginUpdate()
                AddNode(Me.WebBrowser1.Document.Body.Parent, .Nodes)
                .EndUpdate()
                .ExpandAll()
            End With
 
It worked perfectly. Thanks!

This is to show the importance of knowledge about algorythms.

When I was writing the code, I thought on nesting lots of "For ... Next" loops, but there would be never a certain maximum depth to reach. Recursion is a powerful tool in that case.

Thank you very much!
 

Latest posts

Back
Top