Windows Develop Bookmark and Share   
 index > Windows Forms General > HTML DOM tree view from Webbrowser.Document.DomDocument
 

HTML DOM tree view from Webbrowser.Document.DomDocument


  private void UpdateDOM(IHTMLDocument3 HTMLDocument)
        {
            treeView1.Nodes.Clear();
            treeView1.BeginUpdate();
            IHTMLElement domRoot = HTMLDocument.documentElement;
            Node treeRoot = new Node();
            treeRoot.Text = "<html>";
            treeRoot.Tag = domRoot;
            treeView1.Nodes.Add(treeRoot);
            CopyElementToTree(domRoot, treeRoot, this.progressBar1);
            treeView1.EndUpdate();
            treeView1.ExpandAll();
        }
        private static void CopyElementToTree(IHTMLElement element, Node treeNode, ProgressBar pBar)
        {
            IHTMLElementCollection children = (IHTMLElementCollection)element.children;
            if (pBar.Value < 98)
            {
                pBar.Value = pBar.Value + 1;
            }
            if (children.length != 0)
            {
                foreach (IHTMLElement e in children)
                {
                        Node treeChild = new Node();
                        treeChild.Text = getAttrs(e);
                        treeChild.Tag = e;
                        treeNode.Nodes.Add(treeChild);
                        CopyElementToTree(e, treeChild, pBar);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(element.innerText))
                {
                    Node textNode = new Node();
                    textNode.Text = string.Format("\"{0}\"", element.innerHTML);
                    textNode.Tag = element;
                    treeNode.Nodes.Add(textNode);
                }
            }
        }

 private static String getAttrs(IHTMLElement element)
        {
            StringBuilder result = new StringBuilder("<");
            result.Append(element.tagName.ToLower());
            //result.Append(element.innerHTML);
            IHTMLDOMNode domnode = (IHTMLDOMNode)element;
            IHTMLAttributeCollection attrs = (IHTMLAttributeCollection)domnode.attributes;
            if (attrs != null)
            {
                foreach (IHTMLDOMAttribute attr in attrs)
                {
                    if (!attr.specified)
                        continue;
                    result.Append(" ");
                    string nodeValue = string.Empty;
                    if (attr.nodeValue != null)
                    {
                        nodeValue = attr.nodeValue.ToString();
                    }
                    result.Append(string.Format("{0}=\"{1}\"", attr.nodeName, nodeValue));
                }
            }
            result.Append(">");
            return result.ToString();
        }
I use the code below to create treeview from the control Webbrowser.Document.DomDocument.
I wanna get all the information of the HTML DOM,
I know the Website:http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/87bc60d2-51cf-4d1e-8fbe-71b4bdf8e705 to create the treeview but it can not show all the infomation of the HTML .
So, I write the Code below, but if the Document.DomDocument is lagarger the Method of create the tree is sloooower
Could give me some advice about the code ?

Best Regards.
Reynard,Song
Reynard,Song  Wednesday, July 22, 2009 9:24 AM

Hi Reynard,

The problem you met is very common. I have met the same issue when I bind some data from database to a tree view. As far as I know, there is no direct way to speed this process. But we can change the binding way to reduce the count of the adding nodes when the tree view is first loaded. We only need to add the visible nodes and the children of these nodes. When one visible node is expanded, their children would be visible, then we begin to add nodes to their children.

These are the detail steps to add nodes to the tree view when it is loaded at the first time:

1. Save the DomDocument to a variable so that we can access it later.

2. Add the root nodes whose children are added to the tree view. The reason why we add the root nodes is because they ought to be visible. The reason why we add the children of the root nodes is because the root node need to show an ��tag if it has children.

We also need to handle BeforeExpand event to add nodes to the children of the expand node. We need to record the expand state of the added node to avoid duplicated adding. This is a code snippet:
void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)

{

foreach(TreeNode childNode in e.Node.Nodes)

{

//check if the nodes of childNode is added.

if (childNode.Tag == null || (bool)childNode.Tag == false)

{

//TODO:Add nodes to childNode here

//Save the state to indicate the nodes of childNode is already added.

childNode.Tag = true;

}

}

}

Please pay attention to this line //TODO:Add nodes to childNode here , you can write code about adding nodes here.

By the way, although the TreeView in WinForm does not support data binding, but the TreeView in WPF supports it. Since I am not familiar with WPF, I cannot give you more advice about it. You can ask for help in WPF forum.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, July 23, 2009 11:08 AM

Hi Reynard,

The problem you met is very common. I have met the same issue when I bind some data from database to a tree view. As far as I know, there is no direct way to speed this process. But we can change the binding way to reduce the count of the adding nodes when the tree view is first loaded. We only need to add the visible nodes and the children of these nodes. When one visible node is expanded, their children would be visible, then we begin to add nodes to their children.

These are the detail steps to add nodes to the tree view when it is loaded at the first time:

1. Save the DomDocument to a variable so that we can access it later.

2. Add the root nodes whose children are added to the tree view. The reason why we add the root nodes is because they ought to be visible. The reason why we add the children of the root nodes is because the root node need to show an ��tag if it has children.

We also need to handle BeforeExpand event to add nodes to the children of the expand node. We need to record the expand state of the added node to avoid duplicated adding. This is a code snippet:
void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)

{

foreach(TreeNode childNode in e.Node.Nodes)

{

//check if the nodes of childNode is added.

if (childNode.Tag == null || (bool)childNode.Tag == false)

{

//TODO:Add nodes to childNode here

//Save the state to indicate the nodes of childNode is already added.

childNode.Tag = true;

}

}

}

Please pay attention to this line //TODO:Add nodes to childNode here , you can write code about adding nodes here.

By the way, although the TreeView in WinForm does not support data binding, but the TreeView in WPF supports it. Since I am not familiar with WPF, I cannot give you more advice about it. You can ask for help in WPF forum.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, July 23, 2009 11:08 AM

Hi Reynard,

Could you please let me know if my reply helps you? If you still have problems, please feel free to tell me.

Regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, July 27, 2009 3:53 AM

You can use google to search for other answers

Custom Search

More Threads

• Adding an existing form
• Handling NAN, -Infinity, +Infinity in case of mathematical calculations
• Can a dataset be created without a database and bound to a ListView?
• Exception Handling in an Event Handler
• VB at the movies - great for Newbies (good anyway)
• Move a textbox at runtime?
• But What About VB.NET?
• Layered PictureBoxes with Transparent Backgrounds.
• ClientRectangle.Width C#
• Newb: Neat Text Display In Windows Forms