我的原始输入文件文本文件包含一个字符串:
Café (Should be Café)
文本文件是UTF8文件。
输出让我们说是另一个文本文件,因此它不一定适用于网页。
我可以使用哪种C#方法输出正确的格式Café
?
答案 0 :(得分:4)
你试过System.Web.HttpUtility.HtmlDecode("Café")
吗?它返回538M结果
答案 1 :(得分:2)
这是HTML编码的文本。你需要解码它:
string decoded = HttpUtility.HtmlDecode(text);
更新:法语符号“é”包含HTML代码“é
”,因此您需要修改输入字符串。
答案 2 :(得分:2)
使用XML文件时应使用SecurityElement.Escape。
HtmlEncode
将编码许多不需要的额外实体。 XML只要求你转义>,<,&,“和',SecurityElement.Escape
。
当通过XML解析器读回文件时,解析器会为您完成此转换,您不需要“解码”它。
编辑:当然,这仅在编写 XML文件时有用。
答案 3 :(得分:0)
我认为这有效:
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);
答案 4 :(得分:0)
使用HttpUtility.HtmlDecode
。例如:
class Program
{
static void Main()
{
XDocument doc = new XDocument(new XElement("test",
HttpUtility.HtmlDecode("café")));
Console.WriteLine(doc);
Console.ReadKey();
}
}