htmllagilitypack并没有删除所有的html标签,我该如何有效地解决这个问题呢?

本文关键字:解决 有效地 问题 删除 并没有 标签 html htmllagilitypack | 更新日期: 2023-09-27 18:16:44

我使用以下方法从字符串中剥离所有html:

public static string StripHtmlTags(string html)
        {
            if (String.IsNullOrEmpty(html)) return "";
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);
            return doc.DocumentNode.InnerText;
        }

但是它似乎忽略了下面这个标签:[…]

字符串返回的是:

> A hungry thief who stole a rack of pork ribs from a grocery store has
> been sentenced to spend 50 years in prison. Willie Smith Ward felt the
> full force of the law after being convicted of the crime in Waco,
> Texas, on Wednesday. The 43-year-old may feel slightly aggrieved over
> the severity of the […]

我如何确保这些类型的标签被剥离?

htmllagilitypack并没有删除所有的html标签,我该如何有效地解决这个问题呢?

尝试HttpUtility.HtmlDecode

public static string StripHtmlTags(string html)
{
    if (String.IsNullOrEmpty(html)) return "";
    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    doc.LoadHtml(html);
    return HttpUtility.HtmlDecode(doc.DocumentNode.InnerText);
}

HtmlDecode将[…]转换为[…]