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

时间:2014-05-11 07:42:23

标签: c# javascript gecko geckofx

在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); }

您是否有其他建议在C#中实现此目的(没有Javascript)?

1 个答案:

答案 0 :(得分: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();
}