2014-05-11 38 views
1

在C#viaGeckoFx中,我還沒有找到找到元素的所有屬性的方法。我如何獲得GeckoFX/C的所有HTML屬性#

要做到這一點,我做了一個JavaScript函數。這裏是我的代碼

GeckoWebBrowser GeckoBrowser = ....; 
GeckoNode NodeElement = ....; // HTML element where to find all HTML attributes 

string JSresult = ""; 
string JStext = @" 
function getElementAttributes(element) 
{ 
    var AttributesAssocArray = {}; 
    for (var index = 0; index < element.attributes.length; ++index) { AttributesAssocArray[element.attributes[index].name] = element.attributes[index].value; }; 
    return JSON.stringify(AttributesAssocArray); 
} 

getElementAttributes(this); 
"; 

using (AutoJSContext JScontext = new AutoJSContext(GeckoBrowser.Window.JSContext)) { JScontext.EvaluateScript(JStext, (nsISupports)NodeElement.DomObject, out JSresult); } 

你有其他的建議,以實現這個在C#(沒有Javascript)?

回答

1

屬性GeckoElement.Attributes允許訪問元素屬性。

因此,例如,(這是未經測試和未編譯的代碼):

public string GetElementAttributes(GeckoElement element) 
{ 
    var result = new StringBuilder(); 
    foreach(var a in element.Attributes) 
    { 
     result.Append(String.Format(" {0} = '{1}' ", a.NodeName, a.NodeValue)); 
    } 

    return result.ToString(); 
} 
+0

感謝的對你有所幫助湯姆但我行'的foreach(在element.Attributes風險價值)''了System.InvalidCastException'。異常是'無法將'System .__ ComObject'類型的COM對象轉換爲接口類型'Gecko.nsIDOMAttr'。 – LeMoussel

+0

我剛剛用geckofx 29測試了代碼,並且它爲我工作。 (在更新次代碼錯字之後)。你使用的是什麼版本的geckofx?你使用的是匹配的xulrunner/firefox版本嗎? – Tom

+0

我使用了Geckofx-Core.dll和Geckofx-Winforms.dll的29.0.0.2版本。 我已更新至29.0.0.4版。沒關係。 – LeMoussel

相關問題