---
title: "How to: Access the HTML Source in the Managed HTML Document Object Model"
ms.date: "03/30/2017"
dev_langs: 
  - "csharp"
  - "vb"
helpviewer_keywords: 
  - "managed HTML DOM"
  - "HTML [Windows Forms], accessing in Windows Forms"
ms.assetid: 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](how-to-add-web-browser-capabilities-to-a-windows-forms-application.md).  
  
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-csharp[DisplayWebBrowserCode#1](~/samples/snippets/csharp/VS_Snippets_Winforms/DisplayWebBrowserCode/CS/CodeForm.cs#1)]
     [!code-vb[DisplayWebBrowserCode#1](~/samples/snippets/visualbasic/VS_Snippets_Winforms/DisplayWebBrowserCode/VB/CodeForm.vb#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](https://docs.microsoft.com/dotnet/standard/events/index).  
  
7. Add the following code to the <xref:System.Windows.Forms.Control.Click> event handler.  
  
     [!code-csharp[DisplayWebBrowserCode#2](~/samples/snippets/csharp/VS_Snippets_Winforms/DisplayWebBrowserCode/CS/Form1.cs#2)]
     [!code-vb[DisplayWebBrowserCode#2](~/samples/snippets/visualbasic/VS_Snippets_Winforms/DisplayWebBrowserCode/VB/Form1.vb#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

- [Using the Managed HTML Document Object Model](using-the-managed-html-document-object-model.md)
- [WebBrowser Control Overview](webbrowser-control-overview.md)
