htmlagility "treeview" code example

Example: htmlagility treeview

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);
    }
}

Tags:

Html Example