简体   繁体   中英

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

In C# viaGeckoFx, I have not found a method to find all attributes of an element.

To do this, I made ​​a JavaScript function. Here is my code

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

Do you have others suggestions to achieve this in C# (with no Javascript)?

The property GeckoElement.Attributes allows access to an elements attributes.

So for example (this is untested and uncompiled code):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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