如何在HTML中编码特殊字符但排除标签 [英] How to encode special characters in HTML but exclude tags
本文介绍了如何在HTML中编码特殊字符但排除标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将HTML页面转换为包含适当命名实体的页面,将引号,双引号转换为实体.我尝试了以下有效的代码,但对HTML标记进行了编码,我想不加赘述.任何想法如何做到这一点?
I'm trying to convert an HTML page into one containing proper named entities, converting quotes, double quotes to entities. I tried the following code which works but encodes the HTML tags, which I want to leave alone. Any ideas how to do this?
public static string HtmlEncode(string text)
{
string result;
using (StringWriter sw = new StringWriter())
{
var x = new HtmlTextWriter(sw);
x.WriteEncodedText(text);
result = sw.ToString();
}
return result;
}
推荐答案
As noted by MatthewG above, the answer had already been posted to html entity encode text only, not html tag - the solution is to use HTMLAgilityPack and pass the html text or node to the method entitize - this encodes only the page content and not the tags.
using HtmlAgilityPack;
html = HtmlEntity.Entitize(html);
这篇关于如何在HTML中编码特殊字符但排除标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文