.html file > HTMLAgilityPack > TreeView Hierarchy

c# html html-agility-pack treeview

Question

Using HTMLAgilityPack + TreeView to create a hierarchy GUI of an HTML file. The HTML file is CMS-generated, and not very well generated at that.

Needs to do the following: 1. Read each Outline Level 0 node into the TreeView.

HtmlNodeCollection ZeroLevelNodes = doc.DocumentNode.SelectNodes("//body/object[@type='text/sitemap']|//body/ul/object[@type='text/sitemap']|//body/ul/li/object[@type='text/sitemap']");

2. Read the Outline Level 1 nodes as children of their respective Outline Level 0 nodes Note: Each Outline Level 0 node (minus a few that aren't important for this question) are layed out as <li><object><param /></object> (notice the lack of a closing </li> tag). The nodes that need to appear as children in the TreeView will be in an unordered list <ul> that is the next sibling of a level 0 node's <li> tag, for example:

<ul>
    <li>
        <object>
            <param name="**exampleLevel0**" value="**example.htm**" /> //value example.htm as the text in the level 0 node.
        </object>
    <ul>
        <li>
            <object>
               <param name="**ExampleLevel1**" value="childnode.htm" /> //childnode.htm as the text in the level 1 child node.
            </object>
        <li>
            <object>
               <param name="**ExampleLevel1_2**" value="childnode2.htm" /> //childnode2.htm as the text in the level 1 child node.
            </object>
    </ul>
 </ul>

Here is my current code to generate the top-level of hierarchy

HtmlNodeCollection tocNodes = doc.DocumentNode.SelectNodes("//body/object[@type='text/sitemap']|//body/ul/object[@type='text/sitemap']|//body/ul/li/object[@type='text/sitemap']");
foreach (HtmlNode zeroLevelNode in zeroLevelNodes)
        {

            TreeNode tNode = new TreeNode();
            HtmlNode paramNode = zeroLevelNode.SelectSingleNode("param[@name]");
            string paramName = paramNode.GetAttributeValue("name", null);
            string paramValue = paramNode.GetAttributeValue("value", null);
            TreeView.Nodes.Add(new TreeNode(paramValue));
            tNode = TreeView.Nodes[i];
            AddNode(zeroLevelNode, tNode);
            i += 1;
        }

Could anyone give me a hand with the code needed to load the "children" (outline level 1) nodes into the TreeView? (AddNode(zeroLevelNode, tNode)

Popular Answer

Something like this?

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
TreeNode root = new TreeNode("HTML");
treeView1.Nodes.Add(root);

LoadTree(root, doc.DocumentNode);


void LoadTree(TreeNode treeNode, HtmlAgilityPack.HtmlNode rootNode)
{
    foreach (var node in rootNode.ChildNodes.Where(n=>n.Name!="#text"))
    {
        TreeNode n = new TreeNode(node.Name);
        node.Attributes.Select(a => a.Name + "=" + a.Value)
                       .ToList()
                       .ForEach(x => n.Nodes.Add(x));
        treeNode.Nodes.Add(n);

        LoadTree(n, node);
    }
}


Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow