I am writing an application that crawls a group of my web pages. Rather than take the entire source code of the page I'd like to take all of the content and store that and be able to store the page as plain text within a database. The content will be used in other applications and not read by users so there's no need for it to be perfectly human-readable.
At first, I was thinking of using regular expressions, but I have no control over the validity of the web pages and there is a great chance that no regular expression would give me the content.
If I have the source code within a string, how can I turn that string of source code into just the content in C#?
Below function will help to remove all HTML tags, scripts, css, styles from html string and convert it to a plain text. view source
Please, please do not parse HTML yourself! You cannot use just a standard regex to parse HTML - it's not possible.
There are tons of free libraries out there. One of the best free ones in the world of .NET is the HTML Agility Pack.
HTML Agility Pack supports malformed documents as well, which is something that a regex or other basic parsing such as XML will almost never do.
I wrote code to strip out the raw text from markup and present it in my article Convert HTML to Text. The code presented is pretty simple and lightweight.
I also wrote a lightweight HTML parser and have posted it on Github as HTML Monkey. This would be a more complete solution and it would be a simple task to convert the parsed markup to get only the text. I'm still working on this project and am looking for feedback on how it works.
It isn't 100% clear what you want, but I'm assuming you want the text minus markup; so: