C# HtmlDecode Specific tags only

后端 未结 3 1964
忘了有多久
忘了有多久 2021-01-17 00:53

I have a large htmlencoded string and i want decode only specific whitelisted html tags.

Is there a way to do this in c#, WebUtility.HtmlDecode() decodes everything.

3条回答
  •  旧时难觅i
    2021-01-17 01:48

    Or you could use HtmlAgility with a black list or white list based on your requirement. I'm using black listed approach. My black listed tag is store in a text file, for example "script|img"

    public static string DecodeSpecificTags(this string content, List blackListedTags)
        {
            if (string.IsNullOrEmpty(content))
            {
                return content;
            }
            blackListedTags = blackListedTags.Select(t => t.ToLowerInvariant()).ToList();
            var decodedContent = HttpUtility.HtmlDecode(content);
            var document = new HtmlDocument();
            document.LoadHtml(decodedContent);
            decodedContent = blackListedTags.Select(blackListedTag => document.DocumentNode.Descendants(blackListedTag))
                    .Aggregate(decodedContent,
                        (current1, nodes) =>
                            nodes.Select(htmlNode => htmlNode.WriteTo())
                                .Aggregate(current1,
                                    (current, nodeContent) =>
                                        current.Replace(nodeContent, HttpUtility.HtmlEncode(nodeContent))));
            return decodedContent;
        }
    

提交回复
热议问题