日期:2010-05-18  浏览次数:20418 次

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