<?xml version="1.0" encoding="UTF-8"?>
<code>
  <code>public static class HtmlHelper {
    public static string StripHtmlComments(string html) {
        if (html == null) {
            throw new ArgumentNullException("html");
        }

        if (html.IndexOf("&lt;!", StringComparison.Ordinal) &lt; 0) {
            return html;
        }

        var cleanedHtml = new char[html.Length];
        bool inHtmlComment = false;
        bool inHtmlTag = false;
        int cleanCount = 0;

        for (int i = 0; i &lt; html.Length; i++) {
            char current = html[i];

            if (!inHtmlComment &amp;&amp; !inHtmlTag) {
                if (current == '&lt;') {
                    if (i + 1 &lt; html.Length) {
                        char nextChar = html[i + 1];
                        if (nextChar == '!') {
                                inHtmlComment = true;
                                continue;
                            }
                            else {
                                if (IsEnglishLetter(nextChar)) {
                                    inHtmlTag = true;
                                }
                            }
                        }
                    }
                }
                else if(inHtmlComment) {
                    if (current == '&gt;') {
                        if (inHtmlComment) {
                            inHtmlComment = false;
                            continue;
                        }
                    }
                    continue;
                }
                else if (inHtmlTag) {
                    if (current == '&gt;') {
                        inHtmlTag = false;
                    }
                }

                cleanedHtml[cleanCount++] = current;
            }

            return new String(cleanedHtml, 0, cleanCount);
        }

        private static bool IsEnglishLetter(char nextChar) {
            return ('a' &lt;= nextChar &amp;&amp; nextChar &lt;= 'z') || ('A' &lt;= nextChar &amp;&amp; nextChar &lt;= 'Z');
        }

}

//Unit Tests
[TestMethod]
public void NullStringReturnsThrowsArgumentNullException() {
    try {
        HtmlHelper.StripHtmlComments(null);
        Assert.Fail();
    }
    catch (ArgumentNullException) { 
    }
}

[TestMethod]
public void EmptyStringReturnsEmpty() {
    Assert.AreEqual(string.Empty, HtmlHelper.StripHtmlComments(string.Empty));
}

[TestMethod]
public void StringWithoutCommentReturnsSameString() {
    string s = "This has &lt;strong&gt;No Comments&lt;/strong&gt;!";
    Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void StringWithOnlyCommentReturnsEmptyString() {
    string s = "&lt;!-- this go bye bye&gt;";
    Assert.AreEqual(string.Empty, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithNonDashDashComment_ReturnsEmptyString() {
    string s = "&lt;! this go bye bye&gt;";
    Assert.AreEqual(string.Empty, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void StringWithTwoConsecutiveCommentsReturnsEmptyString() {
    string s = "&lt;!-- this go bye bye&gt;&lt;!-- another comment&gt;";
    Assert.AreEqual(string.Empty, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void CommentWithStringBeforeReturnsString() {
    string s = "Hello&lt;!-- this go bye bye --&gt;";
    Assert.AreEqual("Hello", HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void CommentWithStringAfterReturnsString() {
    string s = "&lt;!-- this go bye bye --&gt;World";
    Assert.AreEqual("World", HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithAngleBracketsButNotHtml_NotSripped() {
    string s = "&lt;$)*(@&amp;$(@*&gt;";
    Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithCommentInterleavedWithText_RendersText() {
    string s = "Hello &lt;!-- this go bye bye --&gt; World &lt;!--&gt; This is fun";
    Assert.AreEqual("Hello  World  This is fun", HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithHtmlTags_DoesNotStripHtml() {
    string s = "&lt;strong&gt;Hello&lt;/strong&gt;&lt;!this go bye bye&gt;";
    Assert.AreEqual("&lt;strong&gt;Hello&lt;/strong&gt;", HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithCommentInAttribute_DoesNotStripAttributeValue() {
    string s = "&lt;img alt=\"&lt;!-- This should remain --&gt;\" /&gt;";
    Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithCommentInSingleQuotedAttribute_DoesNotStripAttributeValue() {
    string s = "&lt;img alt=\'&lt;!-- This should remain --&gt;\' /&gt;";
    Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithCommentInNonQuotedAttribute_DoesNotStripAttributeValue() {
    string s = "&lt;p title=&lt;!--Thisshouldremain--&gt;Test&lt;/p&gt;";
    Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithCommentBetweenNonTagButLooksLikeTag_DoesStripComment() {
    string s = @"&lt;&#231;123 title=""&lt;!bc def&gt;""&gt;";
    Assert.AreEqual(@"&lt;&#231;123 title=""""&gt;", HtmlHelper.StripHtmlComments(s));
}

</code>
  <comment>Procedural style code to strip HTML comments. No attempt was made to make this code more OO/patterned base so it could be readable/maintainable. ;)</comment>
  <created-at type="datetime">2008-11-11T01:14:04+00:00</created-at>
  <id type="integer">597</id>
  <language>C#</language>
  <permalink>strip-html-comments</permalink>
  <refactors-count type="integer">4</refactors-count>
  <title>Strip Html Comments</title>
  <trackback-url></trackback-url>
  <updated-at type="datetime">2010-02-03T01:50:47+00:00</updated-at>
  <user-id type="integer">1151</user-id>
  <refactors type="array">
    <refactor>
      <code>public static class HtmlHelper {
    public static string StripHtmlComments(string html) {
        if (html == null) {
            throw new ArgumentNullException("html");
        }

        System.Text.RegularExpressions.Regex regex =
             new System.Text.RegularExpressions.Regex("((&lt;!-- )((?!&lt;!-- ).)*( --&gt;))");

        return regex.Replace(html, string.Empty);
        }
}
</code>
      <code-id type="integer">597</code-id>
      <comment>haven't tested -- but if it doesn't work it'll just be a case of reviewing the regex</comment>
      <created-at type="datetime">2008-11-11T04:52:44+00:00</created-at>
      <id type="integer">67280</id>
      <language>C#</language>
      <rating type="integer">4</rating>
      <ratings-count type="integer">1</ratings-count>
      <title>On Strip Html Comments</title>
      <user-id type="integer">309</user-id>
      <user-name>Elij</user-name>
      <user-website nil="true"></user-website>
    </refactor>
    <refactor>
      <code>public static class HtmlHelper
{
    public static string StripHtmlComments(string pHtml)
    {
        if (pHtml == null)
            throw new ArgumentNullException("pHtml");

        // Contains cleaned content
        StringBuilder oSb = new StringBuilder();

        // Not comment --&gt; skip
        if (pHtml.IndexOf("&lt;!", StringComparison.Ordinal) &lt; 0)
            oSb.Append(pHtml);

        // Not comment --&gt; skip
        if (oSb.Length == 0)
        {
            bool InHtmlComment = false;
            bool InHtmlTag = false;
            char CurrentChar;
            char NextChar;

            for (int CharIndex = 0; CharIndex &lt; pHtml.Length; CharIndex++)
            {
                CurrentChar = pHtml[CharIndex];

                if (!InHtmlComment &amp;&amp; !InHtmlTag)
                {
                    if (CurrentChar == '&lt;' &amp;&amp; (CharIndex + 1 &lt; pHtml.Length))
                    {
                        NextChar = pHtml[CharIndex + 1];
                        if (NextChar == '!')
                        {
                            InHtmlComment = true;
                            continue;
                        }
                        else if (NextChar.IsEnglishLetter())
                            InHtmlTag = true;
                    }
                }
                else if (InHtmlComment)
                {
                    if (CurrentChar == '&gt;')
                    {
                        if (InHtmlComment)
                        {
                            InHtmlComment = false;
                            continue;
                        }
                    }
                    continue;
                }
                else if (InHtmlTag &amp;&amp; CurrentChar == '&gt;')
                    InHtmlTag = false;

                oSb.Append(CurrentChar);
            }

        }

        // One entry point, one return point
        return oSb.ToString();
    }

    private static bool IsEnglishLetter(this char nextChar)
    {
        return ('a' &lt;= nextChar &amp;&amp; nextChar &lt;= 'z') || ('A' &lt;= nextChar &amp;&amp; nextChar &lt;= 'Z');
    }
}</code>
      <code-id type="integer">597</code-id>
      <comment>I ran the test unit on the regex : 7/15 passed. If you still want to do it with a loop I've cleaned your code a little. I can take a look tomorrow too. I'm too tired to think tonight :)</comment>
      <created-at type="datetime">2008-11-11T05:14:22+00:00</created-at>
      <id type="integer">67296</id>
      <language>C#</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Strip Html Comments</title>
      <user-id type="integer">1025</user-id>
      <user-name>Moonshield</user-name>
      <user-website>www.analystik.ca</user-website>
    </refactor>
    <refactor>
      <code>    public static string StripHtmlComments(string html)
    {
        if (html == null)
        {
            throw new ArgumentNullException("html");
        }

        if (html.IndexOf("&lt;!", StringComparison.Ordinal) &lt; 0)
        {
            return html;
        }

        return Regex.Replace(html, "(?&lt;!='|=\"|=)&lt;![^&gt;]+&gt;", "");
    }</code>
      <code-id type="integer">597</code-id>
      <comment>That "invalid HTML tag" case is really tough, a real edge condition. If you discard that one, it's pretty easy. I'd recommend doing a first pass to remove all invalid HTML tags, first. Like.. er.. &lt;&#231;123&gt;.</comment>
      <created-at type="datetime">2009-11-16T04:25:38+00:00</created-at>
      <id type="integer">361815</id>
      <language>C#</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Strip Html Comments</title>
      <user-id type="integer">648</user-id>
      <user-name>Jeff Atwood</user-name>
      <user-website>http://www.codinghorror.com/blog/</user-website>
    </refactor>
    <refactor>
      <code nil="true"></code>
      <code-id type="integer">597</code-id>
      <comment>Desktop Security 2010 is the new antivirus software which is created according to the up-to-date requirements of computer protection. The main aim of this product is to make your computer usage efficient and secure. Multilevel protection of the Desktop Security 2010 antivirus will provide your data integrity and safety, and master components of the program will prevent computer from infection and hack attacks. Special attention is devoted to the network and internet connections security because it is the most probable way of virus intrusion.
1. For me it became a rescuer after a serious virus infection of my pc. Kaspersky antivirus didn't cure viruses, and NOD antivirus did not even detect them. Desktop Security 2010 detected all the viruses very quickly and suggested to cure them. On the whole for now it has been working at my pc for a couple of months as the main antivirus, I don't have any problems with viruses, just sometimes while surfing the web I get messages about attempting intrusions which my Desktop Security 2010 blocks. I am very satisfied with its work.
2. Earlier I did not consider anything except for NOD antivirus. But recently I've been recovering my data for hours after virus intrusion to my computer, and this case happened several times. I understood that I needed to look for something new because my NOD antivirus became too choosy. Kaspersky antivirus is not a variant for me - each computer with a core duo processor hardly works with it. Dr Web is rather old-fashioned. I heard good recommendations about Desktop Security 2010 - and installed it, and it even cured a few viruses after NOD antivirus. Now I just never remind of my problems with viruses.
3. I think that it is one of the best anti-viruses, at least for me - it provides user-friendly menu, easy settings, it doesn't slow down my pc and its visual environment looks nice. It's a pretty good antivirus.
download Desktop Security 2010 
virus Desktop Security 2010 
antivirus Desktop Security 2010 
trojan Desktop Security 2010 
remove Desktop Security 2010 
buy Desktop Security 2010 
cheap Desktop Security 2010</comment>
      <created-at type="datetime">2010-02-03T01:50:44+00:00</created-at>
      <id type="integer">435343</id>
      <language>C#</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Strip Html Comments</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>asgddhd</user-name>
      <user-website nil="true"></user-website>
    </refactor>
  </refactors>
</code>
