首页 > 开发 > HTML > 正文

C#下从HTML文件生成DOM树

2020-02-03 12:54:03
字体:
来源:转载
供稿:网友

  由于最近需要使用c#进行ie的相关编程,因此特整理出相应的代码出来,下面的代码是访问html,并生成treeview dom书的代码:

private void insertdomnodes(ihtmldomnode parentnode,treenode tree_node)
  {
   if(parentnode.haschildnodes())
   {
    ihtmldomchildrencollection allchild = (ihtmldomchildrencollection)parentnode.childnodes;
    int length = allchild.length;

    for(int i=0;i<length;i++)
    {
     ihtmldomnode child_node = (ihtmldomnode)allchild.item(i);
     treenode tempnode = tree_node.nodes.add(child_node.nodename);
     insertdomnodes(child_node,tempnode);
    }
   }
  }
  //将domnode插入

  private void evipsbrowser_documentcomplete(object sender,  axshdocvw.dwebbrowserevents2_documentcompleteevent e)
  {
   domtreeview.nodes.clear();
   ihtmldocument3 htmldocument =(ihtmldocument3)evipsbrowser.document;
   ihtmldomnode rootdomnode = (ihtmldomnode)htmldocument.documentelement;
   
   treenode root = domtreeview.nodes.add("html");
   insertdomnodes(rootdomnode,root);
  }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表