由于最近需要使用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);
}
新闻热点
疑难解答