Html Encode - CSharp System.Web

CSharp examples for System.Web:Web Page

Description

Html Encode

Demo Code


using System.Net;
using System.IO;//from w w w. jav  a  2  s .  c o  m
using System.Text.RegularExpressions;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System;

public class Main{

        public static string HtmlEncode(string str)
        {
            if (str == null)
            {
                return string.Empty;
            }

            string chrChang;

            str = HttpUtility.HtmlEncode(str);

            str = str.Replace("<", "&lt;");
            str = str.Replace(">", "&gt;");
            str = str.Replace("&", "&amp;");

            chrChang = ((char)13).ToString();
            str = str.Replace(chrChang, "<br/>");

            chrChang = ((char)34).ToString();
            str = str.Replace(chrChang, "&quot;");

            chrChang = ((char)32).ToString();
            str = str.Replace(chrChang, "&nbsp;");

            chrChang = ((char)10).ToString();
            str = str.Replace(chrChang, string.Empty);

            return str;
        }
}

Related Tutorials