当前位置:编程学习 > C#/ASP.NET >>

乱码!现将待处理网页代码写入文本,之后对此htm网页代码进行查找替换,并保存,可为什么再打开时中文全乱码!?

先通过如下代码,将htm网页中的代码全读出来
StreamReader sr = new StreamReader(Application.StartupPath + @"\crowdDialogue.html", Encoding.GetEncoding("gb2312"))
string tmp = sr.ReadToEnd();

之后对原htm网页代码中的标志字符串<!--TTTTXXXX****-->进行查找替换
string b = @"<!--TTTTXXXX****-->";
string c = @"<p>----2013年12月12日 14:13:22----<br />";
tmp = tmp.Replace(b, c);
StreamWriter sw = new StreamWriter(Application.StartupPath + @"\crowdDialogue1.html");
sw.Write(tmp);
sw.Close();

最后将新网页打出来
webBrowser1.Navigate(Application.StartupPath + @"\crowdDialogue1.html");

可为什么中文全乱码???
webbrowser 乱码 htm  htm C# --------------------编程问答-------------------- crowdDialogue1.html
这个文件内容加入mata头 charset=gb2312 --------------------编程问答-------------------- 版本不对吧?? --------------------编程问答--------------------
引用 楼主 a427200 的回复:
先通过如下代码,将htm网页中的代码全读出来
C# code?12StreamReader sr = new StreamReader(Application.StartupPath + @"\crowdDialogue.html", Encoding.GetEncoding("gb2312"))string tmp = sr.ReadToEnd();
之后对原htm……

你好,在原htm网页代码中mata头 charset=gb2312有了,仅替换<!--TTTTXXXX****-->
<html>
<head>
<title>XiaoXin</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css">
<!--
body {
margin-left: 10px;
width: 150px;
line-height: 12pt;
font-size: 9pt;
}
-->
</style><head>
<body>
<!--TTTTXXXX****-->

</body>
</html>
--------------------编程问答--------------------
引用 1 楼 wyd1520 的回复:
crowdDialogue1.html
这个文件内容加入mata头 charset=gb2312

对不起啊,刚应用错了 --------------------编程问答--------------------
引用 2 楼 liangxianly 的回复:
版本不对吧??

你好,不明白,请教? --------------------编程问答-------------------- StreamReader 和 StreamWriter 都会涉及到编码,你可以试试给StreamWriter加上gb2312的编码限制。
如果还不行,可以尝试下把编码都改成UTF8 --------------------编程问答-------------------- charset 不对,看下那个 HTML 的编码 --------------------编程问答-------------------- 我老师曾经教过我,右键,编码,改一下 --------------------编程问答-------------------- 只要保证你的所有编码统一就OK啦 --------------------编程问答-------------------- string b = @"<!--TTTTXXXX****-->";
string c = @"<p>----2013年12月12日 14:13:22----<br />";
tmp = tmp.Replace(b, c);
StreamWriter sw = new StreamWriter(Application.StartupPath + @"\crowdDialogue1.html");
sw.Write(tmp);
sw.Close();

 注意你的StreamWriter sw = new StreamWriter(Application.StartupPath + @"\crowdDialogue1.html");这行代码应该也要添上响应的编码
StreamWriter sw = new StreamWriter(Application.StartupPath + @"\crowdDialogue1.html",Encoding.GetEncoding("gb2312"));
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,