title | description | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||
---|---|---|---|---|---|---|---|---|---|
How to: Access the HTML Source in the Managed HTML Document Object Model |
Learn how to access HTML source in the managed HTML Document Object Model. Properties return the HTML as it existed when it was first displayed. |
03/30/2017 |
|
|
53db79fa-8a5e-448e-88c2-f54ace3860b6 |
The xref:System.Windows.Forms.WebBrowser.DocumentStream%2A and xref:System.Windows.Forms.WebBrowser.DocumentText%2A properties on the xref:System.Windows.Forms.WebBrowser control return the HTML of the current document as it existed when it was first displayed. However, if you modify the page using method and property calls such as xref:System.Windows.Forms.HtmlElement.AppendChild%2A and xref:System.Windows.Forms.HtmlElement.InnerHtml%2A, these changes will not appear when you call xref:System.Windows.Forms.WebBrowser.DocumentStream%2A and xref:System.Windows.Forms.WebBrowser.DocumentText%2A. To obtain the most up-to-date HTML source for the DOM, you must call the xref:System.Windows.Forms.HtmlElement.OuterHtml%2A property on the HTML element.
The following procedure shows how to retrieve the dynamic source and display it in a separate shortcut menu.
-
Create a new Windows Forms application. Start with a single xref:System.Windows.Forms.Form, and call it
Form1
. -
Host the xref:System.Windows.Forms.WebBrowser control in your Windows Forms application, and name it
WebBrowser1
. For more information, see How to: Add Web Browser Capabilities to a Windows Forms Application. -
Create a second xref:System.Windows.Forms.Form in your application called
CodeForm
. -
Add a xref:System.Windows.Forms.RichTextBox control to
CodeForm
and set its xref:System.Windows.Forms.Control.Dock%2A property toFill
. -
Create a public property on
CodeForm
calledCode
.[!code-csharpDisplayWebBrowserCode#1] [!code-vbDisplayWebBrowserCode#1]
-
Add a xref:System.Windows.Forms.Button control named
Button1
to your xref:System.Windows.Forms.Form, and monitor for the xref:System.Windows.Forms.Control.Click event. For details on monitoring events, see Events. -
Add the following code to the xref:System.Windows.Forms.Control.Click event handler.
[!code-csharpDisplayWebBrowserCode#2] [!code-vbDisplayWebBrowserCode#2]
Always test the value of xref:System.Windows.Forms.WebBrowser.Document%2A before attempting to retrieve it. If the current page is not finished loading, xref:System.Windows.Forms.WebBrowser.Document%2A or one or more of its child objects may not be initialized.