• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

asp 搞代码 4年前 (2022-01-03) 37次浏览 已收录 0个评论

在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=theStri来源gaodaima#com搞(代@码网ng.Replace(“
“,”\n”);
return theString;
}

以上就是C#中HTML字符转换函数分享的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C#中HTML字符转换函数分享
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址