C#下从HTML文件生成DOM树

80酷酷网    80kuku.com

  dom

  由于最近需要使用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);
  }

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: