C# HtmlDecode Specific tags only

后端 未结 3 2001
忘了有多久
忘了有多久 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条回答
  •  遇见更好的自我
    2021-01-17 01:40

    You could do something like this

    public string DecodeSpecificTags(List whiteListedTagNames,string encodedInput)
    {
        String regex="";
        foreach(string s in whiteListedTagNames)
        {
            regex="<"+@"\s*/?\s*"+s+".*?"+">";
            encodedInput=Regex.Replace(encodedInput,regex);
        }
        return encodedInput;
    }
    

提交回复
热议问题