问题描述
我有一个大htmlen codeD字符串,我想德code只有特定的白名单中的HTML标记。
有没有办法在C#中要做到这一点,WebUtility.HtmlDe code()德codeS的一切。
`我找德codeSpecificTags的implementaiton(),将通过下面的测试。
[测试]
公共无效德codeSpecificTags_SimpleInput_True()
{
字符串输入=放大器; LT;跨度和放大器; GT;我是&放大器; LT;强烈的颜色=蓝色和放大器; GT;极和放大器; LT; / STRONG&放大器; GT;大&放大器; LT; BR&放大器; GT;人&放大器; LT; /跨度&放大器; gt;中;
字符串输出=放大器; LT;跨度和放大器; GT;我是<强烈的颜色=蓝色>非常< / STRONG>大< BR>人&放大器; LT; / SPAN&放大器; GT;
清单<串GT;白名单=新名单,LT;串>(){强,BR}; Assert.IsTrue(德codeSpecificTags(白名单,输入)==输出);
}`
一个更好的办法是使用一些HTML解析器像Agilitypack或csquery或Nsoup找到一个循环的特定元素和去code吧。
检查一下吧,我做到了使用csquery:
字符串输入=放大器; LT;跨度和放大器; GT;我是&放大器; LT;强烈的颜色=蓝色和放大器; GT;极和放大器; LT; / STRONG&放大器; GT;大&放大器; LT ; BR&放大器; GT;人与放大器; LT; / SPAN&放大器; GT;
字符串输出=放大器; LT;跨度和放大器; GT;我是<强烈的颜色=蓝色>非常< / STRONG>大< BR>人&放大器; LT; / SPAN&放大器; GT;VAR德codeD = HttpUtility.HtmlDe code(输出);
变种EN codeD =输入; // HttpUtility.HtmlEn code(德codeD);Console.WriteLine(EN codeD);
Console.WriteLine(德codeD);VAR DOC = CsQuery.CQ.CreateDocument(德codeD);VAR第= doc.Select(强)联盟(doc.Select(BR))。VAR标签=新的List< KeyValuePair<字符串,字符串>>();
变种计数器= 0;的foreach(在第VAR元素)
{
HttpUtility.HtmlEn code(element.OuterHTML)使用.dump();
VAR键=---+专柜+---;
VAR值= HttpUtility.HtmlDe code(element.OuterHTML);
对VAR =新KeyValuePair<字符串,字符串>(键,值); element.OuterHTML =键;
tags.Add(对);
反++;
}VAR finalstring = HttpUtility.HtmlEn code(doc.Document.Body.InnerHTML);
finalstring.Dump();的foreach(在标签VAR元素)
{
finalstring = finalstring.Replace(element.Key,element.Value);
}Console.WriteLine(finalstring);
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.
`I am looking for an implementaiton of DecodeSpecificTags() that will pass below test.
[Test]
public void DecodeSpecificTags_SimpleInput_True()
{
string input = "<span>i am <strong color=blue>very</strong> big <br>man.</span>";
string output = "<span>i am <strong color=blue>very</strong> big <br>man.</span>";
List<string> whiteList = new List<string>(){ "strong","br" } ;
Assert.IsTrue(DecodeSpecificTags(whiteList,input) == output);
}`
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 <strong color=blue>very</strong> big <br>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<KeyValuePair<string, string>>();
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<String,String>(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);
这篇关于C#HtmlDe code特定的标签只的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!