C# HtmlDecode Specific tags only

后端 未结 3 1965
忘了有多久
忘了有多久 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:35

    A better approach could be to use some html parser like Agilitypack or csquery or Nsoup to find specific elements and decode it in a loop.

    check this for links and examples of parsers

    Check It, i did it using csquery :

    string input = "<span>i am <strong color=blue>very</strong> big <br>man.</span>";
    string output = "<span>i am very big 
    man.</span>"; var decoded = HttpUtility.HtmlDecode(output); var encoded =input ; // HttpUtility.HtmlEncode(decoded); Console.WriteLine(encoded); Console.WriteLine(decoded); var doc=CsQuery.CQ.CreateDocument(decoded); var paras=doc.Select("strong").Union(doc.Select ("br")) ; var tags=new List>(); var counter=0; foreach (var element in paras) { HttpUtility.HtmlEncode(element.OuterHTML).Dump(); var key ="---" + counter + "---"; var value= HttpUtility.HtmlDecode(element.OuterHTML); var pair= new KeyValuePair(key,value); element.OuterHTML = key ; tags.Add(pair); counter++; } var finalstring= HttpUtility.HtmlEncode(doc.Document.Body.InnerHTML); finalstring.Dump(); foreach (var element in tags) { finalstring=finalstring.Replace(element.Key,element.Value); } Console.WriteLine(finalstring);

提交回复
热议问题