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