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

所属分类: 网络编程 / ASP.NET 阅读数: 842
收藏 0 赞 0 分享
因此需要以下函数做转换:
复制代码 代码如下:

///<summary>
///替换html中的特殊字符
///</summary>
///<paramname="theString">需要进行替换的文本。</param>
///<returns>替换完的文本。</returns>
public static string HtmlEncode(string theString)
{
theString=theString.Replace(">","&gt;");
theString=theString.Replace("<","&lt;");
theString=theString.Replace(" ","&nbsp;");
theString=theString.Replace("\"","&quot;");
theString = theString.Replace("\'", "&#39;");
theString=theString.Replace("\n","<br/>");
return theString;
}

///<summary>
///恢复html中的特殊字符
///</summary>
///<paramname="theString">需要恢复的文本。</param>
///<returns>恢复好的文本。</returns>
public static string HtmlDiscode(string theString)
{
theString=theString.Replace("&gt;",">");
theString=theString.Replace("&lt;","<");
theString=theString.Replace("&nbsp;"," ");
theString=theString.Replace("&quot;","\"");
theString = theString.Replace("&#39;", "\'");
theString=theString.Replace("<br/>","\n");
return theString;
}
更多精彩内容其他人还在看

asp.net网站首页根据IP自动跳转指定页面的示例

本文介绍的程序主要实现根据IP地址或地址段或IP所在城市进行自动跳转到指定页面的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

asp.net无法获取iis目录的问题解决方法

本文介绍了Asp.Net无法获取IIS拾取目录的解决办法,需要的朋友可以参考下
收藏 0 赞 0 分享

.net中 发送邮件内容嵌入图片的具体实例

这篇文章主要介绍了.net中 发送邮件内容嵌入图片的具体实例,需要的朋友可以参考下
收藏 0 赞 0 分享

在.net中用CheckBoxList实现单选

用CheckBoxList实现单选的原因是我觉得CheckBoxList控件页面展示效果要好看一些,需要的朋友可以参考下
收藏 0 赞 0 分享

ASP.NET―001:GridView绑定List、页面返回值具体实现

这篇文章主要介绍了ASP.NET―GridView绑定List、页面返回值具体实现,需要的朋友可以参考下
收藏 0 赞 0 分享

Ajax实现异步刷新验证用户名是否已存在的具体方法

由于要做一个注册页面,看到许多网站上都是使用Ajax异步刷新验证用户名是否可用的,所以自己也动手做一个小实例
收藏 0 赞 0 分享

ASP.NET汉字转拼音 - 输入汉字获取其拼音的具体实现

这篇文章主要介绍了ASP.NET汉字转拼音 - 输入汉字获取其拼音的具体实现,需要的朋友可以参考下
收藏 0 赞 0 分享

.Net消息队列的使用方法

这篇文章主要介绍了.Net消息队列的使用方法,需要的朋友可以参考下
收藏 0 赞 0 分享

C# web api返回类型设置为json的两种方法

web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面为大家介绍几种不错的方法
收藏 0 赞 0 分享

asp.net获取网站目录物理路径示例

这篇文章主要介绍了asp.net获取网站目录物理路径的方法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多