Frage

I am looking for an HTML encoder that is a little more thorough than .NET's HtmlEncode method. That method will handle <> but not long dashes, which is causing me problems. Is there a .NET library out there that encodes all special characters into HTML?

Where this:

CHARLOTTE, N.C. (AP) — The Republican National Committee is holding its winter meetings in the North Carolina city where Democrats re-nominated Barack Obama for president four months ago.

Would translate into:

CHARLOTTE, N.C. (AP) &mdash ; The Republican National Committee is holding its winter meetings in the North Carolina city where Democrats re-nominated Barack Obama for president four months ago.

War es hilfreich?

Lösung

I ended up using a poor man's HTML encoder I found somewhere online. This uses .NET's HtmlEncode function and then takes all "High ASCII" characters and converts them to the HTML equivalent &# 000;. Sloppy but effective.

        public static string HtmlEncode(this string input)
    {
        char[] chars = HttpUtility.HtmlEncode(input).ToCharArray();
        StringBuilder result = new StringBuilder(input.Length + (int)(input.Length * 0.1));

        foreach (char c in chars)
        {
            int value = Convert.ToInt32(c);
            if (value > 127)
                result.AppendFormat("&#{0};", value);
            else
                result.Append(c);
        }

        return result.ToString();
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top