how to truncate HTML string without leaving it malformated?

c#

Solution

You can try using the HTML Agility Pack - it will parse out the HTML for you, but you will need to figure out how to produce a truncated version yourself. It should make things a lot easier though.

Problem

I have to display first N (for example say 50 or 100) characters out of entire html string. I have to display well formated html.If i apply simple substring that will get me a malformated html string E.g. Sample string : `"<html><body><a href="http://foo.com">foo</a></body></html>"` trucated string: `"<html><body><a href="http://foo.com">foo<"` This will get me malformated html :( Any ideas on how to achieve this ??

Original source