开发者

Return a utf-8 string with HttpEncoder.HtmlDecode?

开发者 https://www.devze.com 2023-01-23 18:22 出处:网络
I am trying to write a method in C# that takes a string with HTML encoded characters and returns it as utf-8 characters.At first I thought this was already possible with the HttpEncoder.HtmlDecode met

I am trying to write a method in C# that takes a string with HTML encoded characters and returns it as utf-8 characters. At first I thought this was already possible with the HttpEncoder.HtmlDecode method but this returns void and takes TextWriter for output as the second param. I don't really get the TextWriter class. How can I wrap this so that it takes and returns strings? Or is there a batter way?

e.g.

myHTMLDecode("Tania Pérez-Salas Compañia de Danza")

would return the utf-8 string - Tania Pérez-Salas Compañia de Danza

开发者_如何学编程edit - The other problem I am having is that HttpEncoder.HtmlDecode is a protected method. Is there anyway around this?


Use a StringWriter

public static String DecodeHtmlEntities(String s) {
    StringWriter writer = new StringWriter();
    HttpEncoder.HtmlDecode(s, writer);
    return writer.ToString();
}


There is also HttpUtility.HtmlDecode which takes a string and retruns a string. HttpEncoder.HtmlDecode seems to be internal according to the page I linked which I would have thought would make it a pain to use...

0

精彩评论

暂无评论...
验证码 换一张
取 消