---
title: "Problem: Underlying HTML from a selection inside WebBrowser Control produces different HTML than the original page"
url: "https://osherove.com/blog/2006/9/25/problem-underlying-html-from-a-selection-inside-webbrowser-c.md"
canonical: "https://osherove.com/blog/2006/9/25/problem-underlying-html-from-a-selection-inside-webbrowser-c.html"
published: "2006-09-25T14:13:54"
updated: "2006-09-25T14:13:54"
tags: [".NET", ".Net 2.0"]
author: "Roy Osherove"
description: "here's a problem I've been trying to deal with: the WebBrowser control in .NET 2.0 Winforms. I'm trying to get the selected text's underlying HTML representation, and it works, kind of. IHTMLDocument2 doc2 =…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Problem: Underlying HTML from a selection inside WebBrowser Control produces different HTML than the original page

By Roy Osherove September 25, 2006 In [.NET](https://osherove.com/blog/category/net), [.Net 2.0](https://osherove.com/blog/category/net-2-0)

**Historical article** · Originally published September 25, 2006; technical details may be outdated.

here's a problem I've been trying to deal with: the WebBrowser control in .NET 2.0 Winforms.

I'm trying to get the selected text's underlying HTML representation, and it works, kind of.

> IHTMLDocument2 doc2 = browser.Document.DomDocument as IHTMLDocument2;
> IHTMLTxtRange range = doc2.selection.createRange() as IHTMLTxtRange;
>
> string plainTextSelection = range.text;
>
> string **selectionHtmlText** \= browser.DocumentText;
>
> string **fullHtmlText** = browser.DocumentText;

My problem is that the html text from the selection range is **different** from that of the original HTML underlying the whole document. the bolder variable contain different HTML, which obviously produces the same result but one of them was generated dynamically by the web browser control (or so it seems).

So, in the range's html text you might have upper instead of lower cased html tag, different amount of new line chars, different formatting of quotes and double quotes and all sorts of bad stuff including getting the order of html tag attributes different.

Does anyone know of a way to get the "real" HTML from a selection inside the web browser control?

## More in .NET

[All posts in .NET](https://osherove.com/blog/category/net)

- Oct 22, 2006

    ### [How to run web services and sites on the same box as sharepoint (avoiding &quot;404 Page Not Found&quot; errors)](https://osherove.com/blog/2006/10/22/how-to-run-web-services-and-sites-on-the-same-box-as-sharepo.html)

- Sep 26, 2006

    ### [.NET Rocks: Regular Expressions with Roy Osherove](https://osherove.com/blog/2006/9/26/net-rocks-regular-expressions-with-roy-osherove.html)

- Sep 23, 2006

    ### [Faster builds of web projects with VS 2005](https://osherove.com/blog/2006/9/23/faster-builds-of-web-projects-with-vs-2005.html)

- Sep 10, 2006

    ### [Regulazy 1.02 is out and about](https://osherove.com/blog/2006/9/10/regulazy-102-is-out-and-about.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
