How to Load string into mshtml object?

  • Thread starter Thread starter adwooley2
  • Start date Start date
A

adwooley2

Can't seem to find anything that works for me. I have a small bit of
HTML that is stored in a string variable. I want to assign this string
to an mshtml object (IHTMLDocument2 I believe), so that I can traverse
the elements and attributes cleanly.
Does anybody know of a way to load this string into an mshtml object?
I would be beholdin'

Tks,
adwooley2
 
Does anybody know of a way to load this string into an mshtml object?

Are you using .NET 2003 or 2005?
 
adwooley2 said:
Using .NET 2005.

I've done this using the new VS2005 WebBrowser control. It seemed to be a
little more complex than I was expecting, but still do-able.

Unfortunately the control didn't seem to like being instantiated from
scratch in code, so I had to create a form and place a WebBrowser control on
it. I don't know why this was necessary but it was the only way I could get
it to work. With this done (I've called my form WebBrowserForm, and the
WebBrowser control placed upon it is named WebBrowser), the following code
should do what you want:

\\\
Public Function HtmlDoc(ByVal HTML As String) As Windows.Forms.HtmlDocument

'Create an instance of the form containing the WebBrowser control
dim browserForm as New WebBrowserForm

'Set the document text to be our HTML
browserForm.WebBrowser.DocumentText = HTML

'Wait until the document is loaded
Do Until browserForm.WebBrowser.ReadyState = _
WebBrowserReadyState.Loaded _
Or browserForm.WebBrowser.ReadyState = _
WebBrowserReadyState.Complete
Application.DoEvents()
Loop

'Return the document
Return browserForm.WebBrowser.Document

End Function
///

You can then interrogate the DOM using the returned HtmlDocument object.

HTH,
 
Oenone,
Much appreciated.
Yes, I think that this is what I was looking for. And thanks for the
code - that makes it clearer.

Rgds,
adwooley2
 
Back
Top