Skip to content

Latest commit

 

History

History
48 lines (32 loc) · 3.49 KB

how-to-access-the-html-source-in-the-managed-html-document-object-model.md

File metadata and controls

48 lines (32 loc) · 3.49 KB
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
csharp
vb
managed HTML DOM
HTML [Windows Forms], accessing in Windows Forms
53db79fa-8a5e-448e-88c2-f54ace3860b6

How to: Access the HTML Source in the Managed HTML Document Object Model

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.

Retrieving the dynamic source with the OuterHtml property

  1. Create a new Windows Forms application. Start with a single xref:System.Windows.Forms.Form, and call it Form1.

  2. 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.

  3. Create a second xref:System.Windows.Forms.Form in your application called CodeForm.

  4. Add a xref:System.Windows.Forms.RichTextBox control to CodeForm and set its xref:System.Windows.Forms.Control.Dock%2A property to Fill.

  5. Create a public property on CodeForm called Code.

    [!code-csharpDisplayWebBrowserCode#1] [!code-vbDisplayWebBrowserCode#1]

  6. 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.

  7. Add the following code to the xref:System.Windows.Forms.Control.Click event handler.

    [!code-csharpDisplayWebBrowserCode#2] [!code-vbDisplayWebBrowserCode#2]

Robust Programming

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.

See also