public static string closeHTML(string str) ...{ string[] HtmlTag = new string[]...{"p","div","span","table","ul","font","b","u","i","a","h1","h2","h3","h4","h5","h6"}; for(int i=0;i<HtmlTag.Length;i++) ...{ int OpenNum = 0,CloseNum = 0; Regex re = new Regex("<" + HtmlTag[i] + "[^>]*" + ">",RegexOptions.IgnoreCase); MatchCollection m = re.Matches(str); OpenNum = m.Count; re = new Regex("</" + HtmlTag[i] + ">",RegexOptions.IgnoreCase); m = re.Matches(str); CloseNum = m.Count; for(int j=0;j < OpenNum - CloseNum;j++) ...{ str += "</" + HtmlTag[i] + ">"; } } return str; }