因此需要以下函数做转换:
复制代码 代码如下:
///<summary>
///替换html中的特殊字符
///</summary>
///<paramname="theString">需要进行替换的文本。</param>
///<returns>替换完的文本。</returns>
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","<br/>");
return theString;
}
///<summary>
///恢复html中的特殊字符
///</summary>
///<paramname="theString">需要恢复的文本。</param>
///<returns>恢复好的文本。</returns>
public static string HtmlDiscode(string theString)
{
theString=theString.Replace(">",">");
theString=theString.Replace("<","<");
theString=theString.Replace(" "," ");
theString=theString.Replace(""","\"");
theString = theString.Replace("'", "\'");
theString=theString.Replace("<br/>","\n");
return theString;
}
您可能感兴趣的文章:
- C#实现过滤html标签并保留a标签的方法
- 使用C#获取网页HTML源码的例子
- C#中Html.RenderPartial与Html.RenderAction的区别分析
- c#使用htmlagilitypack解析html格式字符串
- c#远程html数据抓取实例分享
- C#下解析HTML的两种方法介绍
- C#将html table 导出成excel实例
- C#中除去所有在HTML元素中标记
- C#实现压缩HTML代码的方法
本文由 华域联盟 原创撰写:华域联盟 » C#中HTML字符转换函数分享
转载请保留出处和原文链接:https://www.cnhackhy.com/43885.htm