由于最近需要使用c#處理ie的相關操作,因此積累了一些代碼,下面的代碼是從html代碼中生成dom treeview的例子:
//該過程將被遞歸調用
//dom_node是當前的html dom節點
//tree_node是當前插入樹的結點
private void insertdomnodes(ihtmldomnode parentnode,treenode tree_node)
{
int sibing=0;//當前結點在兄弟結點之間的順序,所有的結點之間通過","隔開
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);
}
新聞熱點
疑難解答