In C #, when we use HTML, we directly use the webbrower control to display HTML. This is very simple. The only thing to note is that when navigate a URL, it is possible that we will start using it if it is not loaded successfully. Because navigate is asynchronous, after a call, return directly without waiting for the page to load.
For example:
WebBrowser webBrowser = new WebBrowser();Uri uri = new Uri("http://www.google.com.hk/");webBrowser.Navigate(uri);String title = webBrowser.DocumentTitle;
In this case, the value obtained through webbrowser. documenttitle is an empty string.
To obtain the title element of the loaded URL, the simplest way is to process the documentcompleted event of webbrowser. The Code is as follows.
static void Main(string[] args){ WebBrowser webBrowser = new WebBrowser(); webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted); Uri uri = new Uri("http://www.google.com.hk/"); webBrowser.Navigate(uri); String title = webBrowser.DocumentTitle;}void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e){ WebBrowser browser = (WebBrowser)sender; if (browser.ReadyState == WebBrowserReadyState.Complete) { String title = browser.DocumentTitle; }}
Now there is a simple requirement. We need to obtain the content in a known HTML document. How can we directly obtain the content by reading HTML files without using webbrowser.
We need to use a COM component: Microsoft HTML object library. You can get a lot of information by using ihtmldocument2 in this component.
Tip: Right-click the project-> Add reference-> COM and select the COM component. The reference of mshtml can be seen in the project. In fact, the COM component uses X: mshtml in/Windows/system32. DLL.
The following code obtains the title element of a .html file. If you want to obtain other information, you can simply modify the function.
/// <summary>/// Get the html file's title./// </summary>/// <param name="strURL">Specified html file's URL (Path).</param>/// <returns>If the html file has title, return it, otherwise, return empty string.</returns>public static String GetHTMLTitle(String strURL){ if (null == strURL || strURL.Equals(String.Empty)) { return String.Empty; } try { mshtml.HTMLDocumentClass docObject = new mshtml.HTMLDocumentClass(); mshtml.IHTMLDocument2 doc2 = docObject as mshtml.IHTMLDocument2; // Load the .html file specified by strURL StreamReader sr = new StreamReader(strURL, Encoding.UTF8); // Read the .html into a string String html = sr.ReadToEnd(); doc2.write(html); doc2.close(); return doc2.title; } catch (System.Exception ex) { } return String.Empty;}
In addition to directly accessing a .html document, this document can also load a website, such as http://www.google.com.hk/
/// <summary>/// Get the html title from the specified URL or local .html file./// </summary>/// <param name="strURL">Specified URL or local .html file (Path).</param>/// <returns>If the html file has title, return it, otherwise, return empty string.</returns>public static String GetHTMLTitle(String strURL){ if (null == strURL || strURL.Equals(String.Empty)) { return String.Empty; } try { HTMLDocumentClass rootDocument = new HTMLDocumentClass(); IHTMLDocument2 document2 = rootDocument; IHTMLDocument4 document4 = rootDocument; document2.write("<ptml></ptml>"); document2.close(); HTMLDocument myDocument = document4.createDocumentFromUrl(strURL, null) as HTMLDocument; int i = 0; while (myDocument.readyState != "complete") { if (++i > 50) { Console.WriteLine("Time Out!"); } System.Threading.Thread.Sleep(100); System.Windows.Forms.Application.DoEvents(); } return myDocument.title; } catch (System.Exception ex) { } return String.Empty;}
Reference: http://capsulecorp.studio-web.net/tora9/cs/mshtmlhtmldocument.html #