2014-05-11 5 views
1

C# viaGeckoFx에서 요소의 모든 속성을 찾는 방법을 찾지 못했습니다.GeckoFX/C로 모든 HTML 속성을 얻는 방법 #

이렇게하려면 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# (자바 스크립트 없음)에서 이것을 달성하기위한 다른 제안이 있으십니까?

답변

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

당신의 도움 톰에 대한 감사의를하지만 라인'foreach는 (element.Attributes에 var에 A)'에서'System.InvalidCastException'을 얻었다. 예외는 ''System .__ ComObject '유형의 COM 객체를'Gecko.nsIDOMAttr '인터페이스 유형으로 변환 할 수 없습니다. – LeMoussel

+0

나는 방금 geckofx 29로 코드를 테스트했다. (마이너 코드 오타 업데이트 후). geckofx의 어떤 버전을 사용하고 있습니까? 일치하는 xulrunner/firefox 버전을 사용하고 있습니까? – Tom

+0

Geckofx-Core.dll 및 Geckofx-Winforms.dll의 29.0.0.2 버전을 사용했습니다. 버전 29.0.0.4로 업데이트되었습니다. 괜찮아. – LeMoussel