Code in C # to generate DOM TreeView from HTML

Source: Internet
Author: User
Dom|treeview

Because of the recent need to use C # to handle the related operations of IE, some code has been accumulated, the following code is an example of the DOM TreeView generated from the HTML code:


This procedure will be called recursively
Dom_node is the current HTML DOM node
Tree_node is the node that is currently inserted into the tree
private void Insertdomnodes (Ihtmldomnode parentnode,treenode tree_node)
{

int sibing=0;//The order of the current node between sibling nodes, all nodes are separated by ","
if (Parentnode.haschildnodes ())
{
level++;
pathstring = pathstring + "," + level;
Ihtmldomchildrencollection allchild = (ihtmldomchildrencollection) parentnode.childnodes;
int length = Allchild.length;
for (int i=0;i<length;i++)
{
string instring = pathstring;
instring = instring + "," + sibing++;
Ihtmldomnode Child_node = (ihtmldomnode) allchild.item (i);
TreeNode Tempnode = Tree_node. Nodes.Add (Child_node.nodename + "_" +instring);
string tmp =
Insertdomnodes (Child_node,tempnode);
pathstring = instring;
}
}
}
private void Evipsbrowser_documentcomplete (object sender, Axshdocvw.dwebbrowserevents2_documentcompleteevent e)
{
PathString = "0";
level = 0;
DOMTreeView.Nodes.Clear ();
IHTMLDocument3 HTMLDocument = (IHTMLDocument3) evipsbrowser.document;
Ihtmldomnode Rootdomnode = (ihtmldomnode) htmldocument.documentelement;

TreeNode root = DOMTreeView.Nodes.Add ("HTML" + "_" +pathstring);
Insertdomnodes (Rootdomnode,root);
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.