C# 如何使用GeckoFX/C获取所有HTML属性#

C# 如何使用GeckoFX/C获取所有HTML属性#,c#,javascript,gecko,geckofx,C#,Javascript,Gecko,Geckofx,在C#viaGeckoFx中,我没有找到一种方法来查找元素的所有属性 为了做到这一点,我做了​​一个JavaScript函数。这是我的密码 GeckoWebBrowser GeckoBrowser = ....; GeckoNode NodeElement = ....; // HTML element where to find all HTML attributes string JSresult = ""; string JStext = @" function getElementA

在C#viaGeckoFx中,我没有找到一种方法来查找元素的所有属性

为了做到这一点,我做了​​一个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); }
geckowebrowser GeckoBrowser=。。。。;
壁虎节点元素=…..;//查找所有HTML属性的HTML元素
字符串JSresult=“”;
字符串JStext=@”
函数getElementAttributes(元素)
{
var AttributesAssocArray={};
对于(var index=0;index

您是否有其他建议在C#(没有Javascript)中实现这一点?

属性GeckoElement.Attributes允许访问元素属性

例如(这是未经测试且未编译的代码):


感谢您的帮助,Tom,但我在
foreach(element.Attributes中的var a)行中得到了
System.InvalidCastException
。异常情况是
无法将'System.\u ComObject'类型的COM对象强制转换为接口类型'Gecko.nsIDOMAttr
。我刚刚用geckofx 29测试了代码,它对我有效。(在更新次要代码输入错误后)。你用的是什么版本的geckofx?您是否使用了匹配的xulrunner/firefox版本。我使用了29.0.0.2版本的Geckofx-Core.dll和Geckofx-Winforms.dll。我更新到29.0.0.4版。没关系。
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();
}