Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C语言输出带有特殊字符(μ;)的HTML#_C#_Html_Special Characters - Fatal编程技术网

C# 用C语言输出带有特殊字符(μ;)的HTML#

C# 用C语言输出带有特殊字符(μ;)的HTML#,c#,html,special-characters,C#,Html,Special Characters,我使用C#通过以下代码生成带有特殊字符(μ)的HTML表: string text1 = "100 μs"; string text2 = "100 \u00B5s"; string text3 = "100 &#xb5s"; string[] lines = { "<html>", "<body>", text1, text2, text3, "</body>", "</html>" }; string curFile = @"C:\

我使用C#通过以下代码生成带有特殊字符(μ)的HTML表:

string text1 = "100 μs";
string text2 = "100 \u00B5s";
string text3 = "100 &#xb5s";

string[] lines = { "<html>", "<body>", text1, text2, text3, "</body>", "</html>" };

string curFile = @"C:\SpecialCharacter.html";

System.IO.File.WriteAllLines(curFile, lines);
string text1=“100μs”;
字符串text2=“100\u00B5s”;
字符串text3=“100µs”;
字符串[]行={“”,text1,text2,text3,“,”};
字符串curFile=@“C:\SpecialCharacter.html”;
System.IO.File.writeAllines(curFile,行);
输出HTML的源代码如下所示:

<html>
<body>
100 μs
100 µs
100 &#xb5s
</body>
</html>

100μs
100微秒
100µs
最后一个在浏览器中正确显示(100µs)。我想知道是否有更好的方法。也就是说,是否有办法在HTML源代码中实际看到“100µs”(并在浏览器中正确显示)?我知道如果我手工编写HTML,这是可能的,但我不知道如何通过C#实现这一点

谢谢。

默认情况下,使用UTF-8编码文本,但忽略了Unicode,这会使一些浏览器感到困惑。要帮助这些浏览器,一个选项是通过显式指定为编码来包含字节顺序标记:

System.IO.File.WriteAllLines(curFile, lines, Encoding.UTF8);
string head = "<head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'></head>";
// ...
string[] lines = { "<html>", head, "<body>", text1, text2, text3, "</body>", "</html>" };
另一个选项是包含指定UTF-8作为编码的
标记:

System.IO.File.WriteAllLines(curFile, lines, Encoding.UTF8);
string head = "<head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'></head>";
// ...
string[] lines = { "<html>", head, "<body>", text1, text2, text3, "</body>", "</html>" };
stringhead=”“;
// ...
字符串[]行={“”,头,“,”,text1,text2,text3,“,”};

如果有人想知道为什么
text1
text2
的输出不同,那是因为
text1
指定U+03BC(希腊小写字母Mu),而
text2
指定U+00B5(微符号)。