how do I input costum content into the Winforms.WebBrowser?

A

ask josephsen

Hi NewsGroup

Hope you can help me with this. I'm using the
"System.Windows.Forms.WebBrowser" to display a various markuplanguage. And
it works fine pointing the "WebBrowser.Url" to a local or remote uri. But
what I would actually like to do, is to download a webpage as a string,
analyze it, possibly modify it, and then display it on the WebBrowser -
without asking the WebBrowser to download it again. By the "HttpWebRequest"
I can easily retrieve the textual webpage and I assume you could replace the
"WebBrowser.document" with a new "Forms.HtmlDocument", made of the textual
webpage just downloaded. But I cant seem to make it work - how do I create a
new HtmlDocument from a string - that may or may not be wellformed!?

any tips most warmly welcomed

../ask
 
N

Nicholas Paldino [.NET/C# MVP]

The easiest way to do this involves saving the content to a file, and
then having the webbrowser navigate to that file. The problem with this is
that any relative links in the document will be resolved with the file path
as the base url, and not the source where it came from.

If you need to resolve these links to the url that you downloaded the
content from, then you will need to do some work. I find that the easiest
way is to create an implementation of IMoniker, which will return the
contents through an IStream implementation. You can have your
GetDisplayName method of your implementation return the URL that the
document originally came from.

Hope this helps.
 
N

Nicholas Paldino [.NET/C# MVP]

Patric,

This will work, but it won't address the issue with having to resolve
relative references in the document.
 
P

Patric J

No of course not since your webpage is not hosted by the webserver you
pulled it from anymore. Just add a base href to the html string like
this, replace [ and ] with < and >

[base href="[URL]http://spaces.msn.com/members/pjsson/[/URL]"]

[a href="Blog/cns!1p76K4WF1ADMWttSKAc6E-Sg!117.entry">Injecting HTML
code
directly into an Internet Explorer</a]
 
A

ask josephsen

Thank you both very much for the imput.

I assume it is not possible to create a "Forms.HtmlDocument" from a string
which is not welformed?

../ask

Nicholas Paldino said:
The easiest way to do this involves saving the content to a file, and
then having the webbrowser navigate to that file. The problem with this
is that any relative links in the document will be resolved with the file
path as the base url, and not the source where it came from.

If you need to resolve these links to the url that you downloaded the
content from, then you will need to do some work. I find that the easiest
way is to create an implementation of IMoniker, which will return the
contents through an IStream implementation. You can have your
GetDisplayName method of your implementation return the URL that the
document originally came from.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ask josephsen said:
Hi NewsGroup

Hope you can help me with this. I'm using the
"System.Windows.Forms.WebBrowser" to display a various markuplanguage.
And it works fine pointing the "WebBrowser.Url" to a local or remote uri.
But what I would actually like to do, is to download a webpage as a
string, analyze it, possibly modify it, and then display it on the
WebBrowser - without asking the WebBrowser to download it again. By the
"HttpWebRequest" I can easily retrieve the textual webpage and I assume
you could replace the "WebBrowser.document" with a new
"Forms.HtmlDocument", made of the textual webpage just downloaded. But I
cant seem to make it work - how do I create a new HtmlDocument from a
string - that may or may not be wellformed!?

any tips most warmly welcomed

./ask
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top