Convert HTML to Plain Text in VB.NET
Practical VB.NET instructions for converting HTML to plain text locally, checking the result, and fixing common formatting and encoding problems.
Before you start
This task is about receiving an HTML string inside a VB.NET application and turning it into readable plain text. The conversion runs locally in your code. No external website, API key, or online service is required unless your own application already retrieves the HTML from a remote source.
Use a project that targets .NET Framework or .NET 5 or later. For the simplest method shown here, add a reference to System.Web only if you are working in .NET Framework and the reference is already appropriate for the project. In .NET 5 or later, prefer the WebUtility method or a small custom replacement.
Step-by-step instructions
First, decide where the HTML comes from. If the input is already a string variable, pass that variable directly to the conversion routine. If the HTML is loaded from a file or stream, read it into a string first.
For most display-oriented output, start by decoding HTML entities and then remove tags. In .NET Framework, the System.Web.HttpUtility class provides both operations.
Use HtmlDecode to convert entities such as &, <, >, and ' into their normal characters. Do this before stripping tags so that text inside attributes or elements is not lost.
Apply StripHtml after decoding. The example below returns the text that remains when tags are removed from the decoded string.
Example for .NET Framework:
Dim decoded As String = System.Web.HttpUtility.HtmlDecode(htmlInput) Dim plain As String = System.Text.RegularExpressions.Regex.Replace(decoded, "<[^>]*>", String.Empty)
If some line breaks should survive, replace block-level tags with a newline before removing the remaining tags. This helps when the source contains paragraphs, headings, or list items.
Example with basic line breaks:
Dim withBreaks As String = Regex.Replace(decoded, "</(p|div|li|h[1-6])>", Environment.NewLine, RegexOptions.IgnoreCase) Dim plain As String = Regex.Replace(withBreaks, "<[^>]*>", String.Empty)
In .NET 5 or later, use System.Net.WebUtility.HtmlDecode for entity decoding. The tag-stripping part can remain the same regular expression.
If you prefer no regular expressions, loop through the string and copy only characters that are outside angle brackets. This treats the input as text and does not attempt to parse real HTML structure.
Check the output
Run the conversion on a short known input and compare the result. For example, a source fragment such as "<b>Hello</b> & welcome" should become the visible text "Hello & welcome" after decoding and tag removal.
Inspect the plain text for leftover angle brackets or entity names. Leftover brackets usually mean tag removal ran before decoding or the input contains malformed tags. Leftover entity names usually mean the wrong decode method was used or the input was encoded twice.
A regular-expression approach is not an HTML parser. It can leave content from comments, scripts, or style blocks, and it does not guarantee correct whitespace for every document. For casual display or simple cleanup this may be acceptable; for reliable extraction from complex pages, use a dedicated HTML parser.
Common problems and next steps
If you see non-breaking spaces as strange characters, decode first and then normalize them. Replace the decoded non-breaking space character with a regular space before or after strip-down.
If script or style content appears in the result, remove those sections before extracting the visible text. The regular expression alone does not understand which parts of the document should be hidden.
If extra blank lines appear, collapse repeated newline characters and trim the result. This is a formatting adjustment after conversion, not a change to the source.
If the HTML may contain user-supplied content, treat it as data. Do not execute scripts or allow the converted text to be reused in a security-sensitive context without the checks required by that context.
For complex HTML, a parser such as the HTML Agility Pack gives more control over nodes and text extraction. Install the package through NuGet if your project permits it, then select the relevant body or text nodes instead of using a broad regular expression.