user3781458
user3781458

Reputation: 121

Converting a string to an HTML document

is there a way to put a string variable into a new declared htmldocument variable without going through a webbrowser ? i tried this

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim v As New WebClient
    Dim page As String = ""
    Dim ahtm As HtmlDocument = Nothing

    page = v.DownloadString("http://www.google.com")
    ahtm.Body.InnerText = page                              'not working
    ahtm.Write(page)                                        'not working neither

End Sub

Upvotes: 2

Views: 19266

Answers (4)

Tyler Montney
Tyler Montney

Reputation: 1500

Make sure to add a reference to Microsoft.mshtml.

    Private Function GetStatus(ByVal HTMLString As String) As mshtml.HTMLDocumentClass
          Dim htmlDocument As mshtml.IHTMLDocument2 = New mshtml.HTMLDocumentClass()
          htmlDocument.clear()
          htmlDocument.write(HTMLString)
          htmlDocument.close()

          Return htmlDocument
    End Function

You very well could try the HTMLAgility pack, but that's third party. I feel it's better to attempt using what's built in (unless third party is vastly superior in functionality and/or security). Then you have to worry about licensing and updates. Using Windows.Forms.WebBrowser will work too, unless you're multi-threading. I'm sure you could work it out, but it felt too complicated for my needs.

Upvotes: 0

Mike Feltman
Mike Feltman

Reputation: 5176

Try the HTMLAgility Pack.

You can read about it here: http://html-agility-pack.net/

The latest version is available via NuGet.

So you would do something like:

Dim aHTML As New HTMLDocument
aHTML.Load(some string variable) 

Note that you cannot load a URL in this fashion. I'm not sure if you really want to load a URL or if the URL provided was just for reference.

Upvotes: 0

aban-developer
aban-developer

Reputation: 78

You can use WebBrowser without Navigate().

Public Function CreateDocument(ByVal url As String) As HtmlDocument
      Dim wb As New WebBrowser()
      wb.DocumentText=New WebClient().DownloadString(url)
      Return wb.Document
End Function

Upvotes: 3

Aelphaeis
Aelphaeis

Reputation: 2613

The reason is because you declared ahtm as Nothing. instantiate it and see if it works.

Update: HtmlDocument is a wrapper around an unmanaged class (IHtmlDocument). Try Declaring a WebBrower and then assigning the ahtm to the web browser document property.

WebBrowser wb = new WebBrowser();
HtmlDocument atm = wb.Document;

In other words, Web browser is the easiest way.

Update: The alternative would be to use something like HtmlAgilityPack. http://htmlagilitypack.codeplex.com/

Upvotes: 1

Related Questions