public static bool WriteFile(string strText, string strContent, string strAuthor)
    {
        string path = HttpContext.Current.Server.MapPath("Html/");
        System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");        // 读取模板文件
        string temp = HttpContext.Current.Server.MapPath("Html/ActionDetails.html");
        StreamReader sr = null; 
        StreamWriter sw = null; 
        string str = "";
        try
        {
            sr = new StreamReader(temp, code);
            str = sr.ReadToEnd(); // 读取文件
        }
        catch (Exception exp)
        {
            HttpContext.Current.Response.Write(exp.Message);
            HttpContext.Current.Response.End(); 
            sr.Close();
        }
        string htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
        // 替换内容 // 这时,模板文件已经读入到名称为str的变量中了 
        str = str.Replace("$title$", strText); //模板页中的ShowArticle
        str = str.Replace("$time$", strText);
        str = str.Replace("$content$", strContent); 
        // 写文件
        try
        {
            sw = new StreamWriter(path + htmlfilename, false, code); //为false时候是覆盖,true为追加
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sw.Close();
        }
        return true;
    }结果发现生成的Html代码是这样的<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文?/title>
    <link href="../css/css.css" rel="stylesheet" type="text/css" />
</head><body><div class="xxl_moban" id="content_xxl">
   <div id="txt_xxl">
<h2>了解我们最新的印刷工艺与产品生产过程中的解决方案…?/h2>
    <h1>? titititititititle</h1>
    <p>?time$?/p>
    <h3 class="conent">datatadatadatadatadata</h3>
  </div>
</div>
</body>
</html>很多标签都变成了“?”

解决方案 »

  1.   

     这是模板页!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
        <link href="../css/css.css" rel="stylesheet" type="text/css" />
    </head><body><div class="xxl_moban" id="content_xxl">
       <div id="txt_xxl">
    <h2>了解我们最新的印刷工艺与产品生产过程中的解决方案……</h2>
        <h1>◎  $title$</h1>
        <p>【$time$】</p>
        <h3 class="conent">$content$</h3>
      </div>
    </div>
    </body>
    </html>
      

  2.   

    你查检一下传入的string strText, string strContent, string strAuthor的值是否正常吧
      

  3.   


    为了方便调式,我是这么引用的
    WriteFile("titititititititle", "datatadatadatadatadata", "contentcontentcontent");传入的值没有问题。
      

  4.   

    浏览器的 查看——〉字符编码方式是不是也是utf-8
      

  5.   


    看了,是Unicode(utf-8)我想应该不是浏览器的问题吧, 
    有很多标签都变成了“?”,咋回事呢?
      

  6.   

      System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");改成下面试下:
      System.Text.Encoding code = System.Text.Encoding.GetEncoding("utf8");
      

  7.   

    换成System.Text.Encoding.GetEncoding("utf-8");编码
    调试看看str 中内容。
      

  8.   

    或html页面改成
    <META   http-equiv=Content-Type   content= "text/html;   charset=gb2312 "> 
    看看
      

  9.   

    debug 单步执行监视str 
      

  10.   


    public static bool WriteFile(string strText, string strContent, string strAuthor)
        {
            string path = HttpContext.Current.Server.MapPath("Html/");
            System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");        // 读取模板文件............<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    上面是GB2312,下面是UTF8,会是这个问题吗?楼主把编码统一一下试试吧