如何编码 HTML 中的特殊字符但排除标签
How to encode special characters in HTML but exclude tags
我正在尝试将 HTML 页面转换为包含正确命名实体的页面,将引号、双引号转换为实体。我尝试了以下有效的代码,但对 HTML 标签进行了编码,我想单独留下。任何想法如何做到这一点?
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;
}
正如上面 MatthewG 所指出的,答案已经发布到 html entity encode text only, not html tag - 解决方案是使用 HTMLAgilityPack 并将 html 文本或节点传递给方法 entitize - 这仅编码页面内容而不是标签。
using HtmlAgilityPack;
html = HtmlEntity.Entitize(html);
我正在尝试将 HTML 页面转换为包含正确命名实体的页面,将引号、双引号转换为实体。我尝试了以下有效的代码,但对 HTML 标签进行了编码,我想单独留下。任何想法如何做到这一点?
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;
}
正如上面 MatthewG 所指出的,答案已经发布到 html entity encode text only, not html tag - 解决方案是使用 HTMLAgilityPack 并将 html 文本或节点传递给方法 entitize - 这仅编码页面内容而不是标签。
using HtmlAgilityPack;
html = HtmlEntity.Entitize(html);