2012-02-10 41 views
1

如何投射mshtml.IHTMLDivElementmshtml.HTMLDivElementClass如何將mshtml.IHTMLDivElement轉換爲mshtml.HTMLDivElementClass?

IHTMLElementCollection collection = doc.body.all; 

foreach (var htmlElem in collection) 
{ 
    if (htmlElem is mshtml.IHTMLDivElement) 
    { 
     mshtml.IHTMLDivElement div = htmlElem as mshtml.IHTMLDivElement; 
     if (div != null) 
     { 
      // HTMLDivElementClass divClass = (HTMLDivElementClass)div; ?????????      
     } 
    }    
} 

我需要訪問HTMLDivElementClass才能夠獲得它的所有成員。

enter image description here

+0

爲什麼你需要做演員?你註釋掉的代碼有什麼問題? – vcsjones 2012-02-10 15:41:33

+0

@vcsjones因爲我無法訪問HTMLDivElementClass中需要的所有屬性。 IHTMLDivElement只有2個專有名...例如我需要獲得DIV的ID等等...... – 2012-02-10 15:44:08

回答

1

您的代碼幾乎是正確的。不知道你想要什麼。

請更改像下面

mshtml.IHTMLElementCollection collection = (mshtml.IHTMLElementCollection)objDocument.body.all; 

      foreach (var htmlElem in collection) 
      { 
       if (htmlElem is mshtml.IHTMLDivElement) 
       { 
        mshtml.HTMLDivElementClass div = htmlElem as mshtml.HTMLDivElementClass; 
        if (div != null) 
        { 
        //DO YOUR CODE HERE 
        //div.IHTMLElement_id 
        } 
       } 
      } 

這是爲我工作,並在你的代碼「格」對象類型「HTMLDivElementClass」

而且多了一個建議,如果你想給所有的DIV僅從頁面標記,然後使用以下代碼行。

mshtml.IHTMLElementCollection collection = (mshtml.IHTMLElementCollection)objDocument.getElementsByName("div"); 

而不是

mshtml.IHTMLElementCollection collection = (mshtml.IHTMLElementCollection)objDocument.body.all; 

,將刪除你的病情檢查元素是DIV或沒有。

希望對你有所幫助。

相關問題