0
HtmlDocument doc = webBrowser1.Document;   

I can only get the Html document if I browse to a page.

Is it possible to get Html document:

  1. without navigating the webpage?
  2. Without Using Html Agility Pack?
4
  • What do you mean by "navigating the web page"? You don't need to use the HTML Agility Pack, it just makes life easier. Alternatively, you could use regex, but you might end up pulling your hair out. Commented Sep 2, 2012 at 14:28
  • 2
    You can get any page you want by making an HTTP Request. Check out the docs for WebRequest: msdn.microsoft.com/en-us/library/system.net.webrequest.aspx . Commented Sep 2, 2012 at 14:32
  • Thanks for your response. By "navigating" I meant webBrowser1.Navigate("bbc.co.uk/sport"); But I dont want to use a browser to display anything, I just want the webpage in HTMLDocument type available to me. I know about data = client.DownloadString("bbc.co.uk/sport"); but the document is string :( meaning that more of Regular expressions... Commented Sep 2, 2012 at 14:52
  • Never use regex on HTML! Commented Sep 2, 2012 at 15:06

2 Answers 2

4

This is one way of doing that

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
WebBrowser wb = new WebBrowser();
wb.DocumentStream = response.GetResponseStream();
wb.ScriptErrorsSuppressed = true;
HtmlDocument doc = wb.Document;

Same as the WebBrowser control it takes a few seconds for the contents of the stream to populate the control. Also make sure to do proper disposing after you are done.

Sign up to request clarification or add additional context in comments.

Comments

0

You need a documented loaded for there to be a root element. Try loading "about:blank" to get an empty document without relying on any other URL or file.

Comments

Your Answer

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