How do you convert Html to plain text?
If you are talking about tag stripping, it is relatively straight forward if you don't have to worry about things like <script>
tags. If all you need to do is display the text without the tags you can accomplish that with a regular expression:
<[^>]*>
If you do have to worry about <script>
tags and the like then you'll need something a bit more powerful then regular expressions because you need to track state, omething more like a Context Free Grammar (CFG). Althought you might be able to accomplish it with 'Left To Right' or non-greedy matching.
If you can use regular expressions there are many web pages out there with good info:
- http://weblogs.asp.net/rosherove/archive/2003/05/13/6963.aspx
- http://www.google.com/search?hl=en&q=html+tag+stripping+&btnG=Search
If you need the more complex behaviour of a CFG I would suggest using a third party tool, unfortunately I don't know of a good one to recommend.
How can I Convert HTML to Text in C#?
What you are looking for is a text-mode DOM renderer that outputs text, much like Lynx or other Text browsers...This is much harder to do than you would expect.
How to convert html to text without removing html tags
Thanks for all your responses. I was able to do it in angular template instead of getting the converted html from C# using <p [innerHTML]="sampleHtml">'. innerHTML></p>
, innerHTML does not work with 'textarea' which I was trying to do, so I used a paragraph, div can also be used.
HTML to Text Email Converter in C#
You can use HtmlAgilityPack and loop through nodes to extract text. Example: Grab all text from html with Html Agility Pack
How to convert HTML to plain text
You can use HtmlAgilityPack
's HtmlToText demo, which can be found here.
I had a look at the other answers but they all suggest various solutions involving regular expressions. I thought that HtmlAgilityPack
didn't get enough attention.
All you need to do is plug the NuGet package in your project and follow the example.
Related Topics
How to Specify a [Dllimport] Path At Runtime
What Are the Differences Between a Multidimensional Array and an Array of Arrays in C#
How to Create a Custom Authorizeattribute in ASP.NET Core
Use Unity API from Another Thread or Call a Function in the Main Thread
How to Match an Entire String With a Regex
Protect .Net Code from Reverse Engineering
Finding the Variable Name Passed to a Function
Arraylist VS List≪≫ in C#
How to Read a CSV File into a .Net Datatable
Possible to Call C++ Code from C#
How to Deserialize Json With C#
How to Generate a Random Integer in C#
Performance Differences Between Debug and Release Builds
Public Fields Versus Automatic Properties