繁体   English   中英

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

[英]How can I get all HTML attributes with GeckoFX/C#

在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)?

属性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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM