using System.Collections.Generic;
using System.Text.RegularExpressions;
public static void Main()
var html = @"<body><table width=""100%"" border=""0"" cellspacing=""5"" cellpadding=""5""><tr><td style=""height:20px""></td></tr><tr><td><table width=""600"" border=""0"" align=""center"" cellpadding=""0"" cellspacing=""0"" style=""background-color: #FFFFFF; border-bottom: 1px solid #CCCCCC; font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #4D4D4D;""><tr><td height='100px' background=""https://ins-ltp10.instancysoft.com/Content/SiteConfiguration/388/header.gif"" style=""background-image:url('https://ins-ltp10.instancysoft.com/Content/SiteConfiguration/388/header.gif');background-repeat: repeat-y; background-repeat:repeat-x;""> <!--[if gte mso 9]><v:rect xmlns:v=""urn:schemas-microsoft-com:vml"" style=""width:600px;height:100px;"" strokecolor=""none""><v:fill xmlns:v=""urn:schemas-microsoft-com:vml"" type=""tile"" src=""https://ins-ltp10.instancysoft.com/Content/SiteConfiguration/388/header.gif"" color=""#FFFFFF"" /></v:rect><v:shape xmlns:v=""urn:schemas-microsoft-com:vml"" id=""imgEmailHeaderBg"" style=""position:absolute; Left:-10px; Top:-5px; width:600px; height:100px;""><![endif]--><a href=""https://ondemand.vengreso.com/"" title=""Vengreso"" target=""_blank""><img id=""imgSiteLogo"" align=""baseline"" style=""cursor:pointer"" title=""Vengreso"" src=""https://ins-ltp10.instancysoft.com/Content/SiteConfiguration/388/Logo.gif"" border=""0""/></a><!--[if gte mso 9]></v:shape><![endif]--></td></tr><tr><td style=""background-color:#FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #4D4D4D; Padding: 20px;""><p>Dear <span contenteditable=""false"" placeholder=""true"" dbcolumnname=""FirstName"">Srinivas 97</span> <span contenteditable=""false"" placeholder=""true"" dbcolumnname=""LastName"">y</span>, <br /><br />Following are your account details:<br /></p><p><b>Site URL:</b><a placeholder=""true"" dbcolumnname=""CompanyURL"" href=""https://ondemand.vengreso.com/""><span contenteditable=""false"" placeholder=""true"" dbcolumnname=""CompanyName"">Vengreso</span></a></p><ul><li><b>First Name: </b><span contenteditable=""false"" placeholder=""true"" dbcolumnname=""FirstName"">Srinivas 97</span></li><li><b>Last Name: </b><span contenteditable=""false"" placeholder=""true"" dbcolumnname=""LastName"">y</span></li><li><b>User Name: </b><span contenteditable=""false"" placeholder=""true"" dbcolumnname=""Login"">srinuy97@gmail.com</span></li><li><b>Password: </b><span contenteditable=""false"" placeholder=""true"" dbcolumnname=""Password"">abc</span></li></ul><p>Thank You!<br /><span contenteditable=""false"" placeholder=""true"" dbcolumnname=""CompanyName"">Vengreso</span> Support</p></td></tr><tr><td style=""background-color:#FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #4D4D4D; border-top: 1px solid #CCCCCC; Padding: 6px; height: 30px;""> © 2019 Vengreso</td></tr></table></td></tr><tr><td style=""height:20px""></td></tr></table></body>";
var texts = GetTextFromHtml(html);
Console.WriteLine(texts);
private static string GetTextFromHtml(string html)
if (string.IsNullOrEmpty(html))
var htmlDoc = new HtmlDocument();
return GetTextFromNodes(htmlDoc.DocumentNode.ChildNodes);
private static string GetTextFromNodes(HtmlNodeCollection nodes, int indent = 0)
StringBuilder texts = new StringBuilder();
string[] linebreaks = {"p", "br", "table", "th", "tr"};
string[] indentTag = {"ul", "li"};
foreach (var node in nodes)
if (node.Name.ToLowerInvariant() == "style")
if (indentTag.Contains(node.Name.ToLowerInvariant()))
texts.Append(GetTextFromNodes(node.ChildNodes, indent + 1));
texts.Append(GetTextFromNodes(node.ChildNodes, indent));
var innerText = node.InnerText;
if (!string.IsNullOrWhiteSpace(innerText))
texts.Append(new String(' ', indent) + node.InnerText);
if (node.Name.ToLowerInvariant() == "a")
texts.Append("\r\n" + node.Attributes["href"].Value + "\r\n");
if (node.Name.ToLowerInvariant() == "img" && !node.Attributes["src"].Value.EndsWith("invis.gif"))
texts.Append("\r\n" + node.Attributes["src"].Value + "\r\n");
if (linebreaks.Contains(node.Name.ToLowerInvariant()))