58 lines
1.7 KiB
C#
58 lines
1.7 KiB
C#
using System.Globalization;
|
|
using System.Text;
|
|
|
|
namespace FSI.Lib.CompareNetObjects
|
|
{
|
|
/// <summary>
|
|
/// Helper class for web related methods
|
|
/// </summary>
|
|
public static class WebHelper
|
|
{
|
|
/// <summary>
|
|
/// HTML-encodes a string and returns the encoded string.
|
|
/// </summary>
|
|
/// <param name="text">The text string to encode. </param>
|
|
/// <returns>The HTML-encoded text.</returns>
|
|
public static string HtmlEncode(string text)
|
|
{
|
|
if (text == null)
|
|
return null;
|
|
|
|
StringBuilder sb = new StringBuilder(text.Length);
|
|
|
|
int len = text.Length;
|
|
for (int i = 0; i < len; i++)
|
|
{
|
|
switch (text[i])
|
|
{
|
|
case '<':
|
|
sb.Append("<");
|
|
break;
|
|
case '>':
|
|
sb.Append(">");
|
|
break;
|
|
case '"':
|
|
sb.Append(""");
|
|
break;
|
|
case '&':
|
|
sb.Append("&");
|
|
break;
|
|
default:
|
|
if (text[i] > 159)
|
|
{
|
|
// decimal numeric entity
|
|
sb.Append("&#");
|
|
sb.Append(((int)text[i]).ToString(CultureInfo.InvariantCulture));
|
|
sb.Append(";");
|
|
}
|
|
else
|
|
sb.Append(text[i]);
|
|
break;
|
|
}
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
}
|
|
|