title: how to dynamically generate HTML code using .NET's WebBrowser or mshtml.HTMLDocument?
body: <p>Most of the answers I have read concerning this subject point to either the System.Windows.Forms.WebBrowser class or the COM interface mshtml.HTMLDocument from the Microsoft HTML Object Library assembly.</p>
  
  <p>The WebBrowser class did not lead me anywhere. The following code fails to retrieve the HTML code as rendered by my web browser:</p>
  
  <pre><code>[STAThread]
  public static void Main()
  {
      WebBrowser wb = new WebBrowser();
      wb.Navigate("https://www.google.com/#q=where+am+i");
  
      wb.DocumentCompleted += delegate(object sender, WebBrowserDocumentCompletedEventArgs e)
      {
          mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)wb.Document.DomDocument;
          foreach (IHTMLElement element in doc.all)
          {
                      System.Diagnostics.Debug.WriteLine(element.outerHTML);
          }     
      };
      Form f = new Form();
      f.Controls.Add(wb);
      Application.Run(f);
  } 
  </code></pre>
  
  <p>The above is just an example. I'm not really interested in finding a workaround for figuring out the name of the town where I am located. I simply need to understand how to retrieve that kind of dynamically generated data programmatically.</p>
  
  <p>(Call new System.Net.WebClient.DownloadString("<a href="https://www.google.com/#q=where+am+i">https://www.google.com/#q=where+am+i</a>"), save the resulting text somewhere, search for the name of the town where you are currently located, and let me know if you were able to find it.)</p>
  
  <p>But yet when I access "<a href="https://www.google.com/#q=where+am+i">https://www.google.com/#q=where+am+i</a>" from my Web Browser (ie or firefox) I see the name of my town written on the web page. In Firefox, if I right click on the name of the town and select "Inspect Element (Q)" I clearly see the name of the town written in the HTML code which happens to look quite different from the raw HTML that is returned by WebClient.</p>
  
  <p>After I got tired of playing System.Net.WebBrowser, I decided to give mshtml.HTMLDocument a shot, just to end up with the same useless raw HTML:</p>
  
  <pre><code>public static void Main()
  {
      mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)new mshtml.HTMLDocument();
      doc.write(new System.Net.WebClient().DownloadString("https://www.google.com/#q=where+am+i"));
  
      foreach (IHTMLElement e in doc.all)
      {
              System.Diagnostics.Debug.WriteLine(e.outerHTML);
      }
  } 
  </code></pre>
  
  <p>I suppose there must be an elegant way to obtain this kind of information. Right now all I can think of is add a WebBrowser control to a form, have it navigate to the URL in question, send the keys "CLRL, A", and copy whatever happens to be displayed on the page to the clipboard and attempt to parse it. That's horrible solution, though.</p>