簡體   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