如何正确解码重音字符以便显示

我的原始输入文件文本文件包含一个字符串:

Caf&eacute (Should be Café) 

文本文件是UTF8文件。

输出让我们说是另一个文本文件,所以它不一定适用于网页。

我可以使用哪种C#方法输出正确的格式, Café

显然是一个常见的问题 ?

你试过System.Web.HttpUtility.HtmlDecode("Café")吗? 它返回538M结果

这是HTML编码的文本。 你需要解码它:

 string decoded = HttpUtility.HtmlDecode(text); 

更新:法语符号“é”具有HTML代码“ é ”因此,您需要修复输入字符串。

在处理XML文件时,您应该使用SecurityElement.Escape 。

HtmlEncode将编码许多不需要的额外实体。 XML只要求你逃避>,<,&,“和”, SecurityElement.Escape会这样做。

当通过XML解析器读回文件时,解析器会为您完成此转换,您不需要“解码”它。

编辑:当然这只有在编写 XML文件时才有用。

我认为这有效:

 string utf8String = "Your string"; Encoding utf8 = Encoding.UTF8; Encoding unicode = Encoding.Unicode; byte[] utf8Bytes = utf8.GetBytes(utf8String); byte[] unicodeBytes = Encoding.Convert(utf8, unicode, utf8Bytes); char[] uniChars = new char[unicode.GetCharCount(unicodeBytes, 0, unicodeBytes.Length)]; unicode.GetChars(unicodeBytes, 0, unicodeBytes.Length, uniChars, 0); string unicodeString = new string(uniChars); 

使用HttpUtility.HtmlDecode 。 例:

 class Program { static void Main() { XDocument doc = new XDocument(new XElement("test", HttpUtility.HtmlDecode("café"))); Console.WriteLine(doc); Console.ReadKey(); } }