Vaelek
Vaelek

Reputation: 190

How to get html from an element in WebKit.Net

I'm migrating an old project to WebKit.NET 0.5 Cairo. Most roadblocks I've gotten past but I can't figure out how to get the HTML for an individual WebKit.DOM.Element object.

I can read the HTML of the entire document using WebKitBrowser.DocumentText, but that is not what I need.

Below is basically what I'm trying to accomplish. I've browsed through the docs and done a lot of searching and have come up empty on all fronts. I've found no way to accomplish the last line in the snippet.

WebKit.DOM.Element newdiv = webKitBrowser1.Document.CreateElement("DIV");
...
[ Create some SPAN's and append them to newdiv ]
...
String divHTML = newdiv.DocumentText // <-- DocumentText is not a member of WebKit.DOM.Element

With the IE control, I could just do divHTML = newdiv.InnerHTML, but can't find any equivalent to that for WebKit.NET.

As an alternative, I would accept a solution to inject HTML into an element such that I can manually add an IMG tag without creating a DOM element specifically for it. Using divHTML.TextContent, the html is interpreted as plain text.

Thanks in advance!

Upvotes: 4

Views: 2420

Answers (1)

Vaelek
Vaelek

Reputation: 190

5 years later, now that I can answer my own questions I'll formally answer this one as it has sat so long and is flagged as a popular question.

Trial and error has finally led to a less than optimal but functional solution in the form of

WebKit.DOM.Element newdiv = webKitBrowser1.Document.CreateElement("DIV");
...
[ Create some SPAN's and append them to newdiv ]
...
String divHTML = webKitBrowser1.StringByEvaluatingJavaScriptFromString("document.getElementById('DivElementID').innerHTML");

The same method can then also be used to set the innerHTML of an Element.

This also solves an un-mentioned issue of WebKitBrowser.DocumentText reflecting only the original HTML and not any modified results.

Upvotes: 1

Related Questions