C#中HTML字符转换函数分享

在ASP.Net中经常会从网面中取数据或更新网页的显示。因为HTML中有些特殊字符如, &等,显示实际值不一致,造成保存到数据库再取出来时会不一样

因此需要以下函数做转换:
复制代码 代码如下:

///
///替换html中的特殊字符
///

///需要进行替换的文本。
///替换完的文本。
public static string HtmlEncode(string theString)
{
theString=theString.Replace(">",">");
theString=theString.Replace("<","<");
theString=theString.Replace(" "," ");
theString=theString.Replace("\"",""");
theString = theString.Replace("\'", "'");
theString=theString.Replace("\n","
");
return theString;
}

///
///恢复html中的特殊字符
///

///需要恢复的文本。
///恢复好的文本。
public static string HtmlDiscode(string theString)
{
theString=theString.Replace(">",">");
theString=theString.Replace("<","<");
theString=theString.Replace(" "," ");
theString=theString.Replace(""","\"");
theString = theString.Replace("'", "\'");
theString=theString.Replace("
","\n");
return theString;
}

以上就是C#中HTML字符转换函数分享的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » ASP编程