2

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
3
  • Can you elaborate on what you mean by not working? Is it throwing an exception or simply returning null? Commented Jun 27, 2014 at 18:15
  • it's throwing this exception An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication6.exe Commented Jun 27, 2014 at 18:17
  • From the docs for HtmlDocument: "You obtain an instance of HtmlDocument through the Document property of the WebBrowser control." So, no, you can't do it without a WebBrowser control. Note that you don't have to actually display the WebBrowser control. What is the ultimate aim of this? Commented Jun 27, 2014 at 18:49

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

In single threaded applications, sure. Otherwise, you're going to get an STA exception.
1

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/

3 Comments

when i try to do that, it says "System.Windows.Forms.HtmlDocument' has no constructors"
as i said i need to do this without going through a webbrowser
thank you, that actually helped with the htmldocument.loadhtml
0

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.

1 Comment

Could you reference anything specifically related to the question?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.