public static string SiteTemplate()//HTML页的模板
        {
            string str = "";
            str += "<HTML>";
            str += "<head>";
            str += "<title>biaoti</title>";
            str += "</head>";
            str += "<body>";
            str += "biaoti";
            str += "<br>";
            str += "content<br>";
            str += "author";
            str += "</body>";
            str += "</HTML>";//模版页html代码
            return str;
        }
        public static bool WriteFile(string strText, string strContent, string strAuthor)
        {
            //string path = HttpContext.Current.Server.MapPath("/htm/");//文件输出目录,如果有这句还会出异常,是未能映射路径"/htm/"
            Encoding code = Encoding.GetEncoding("utf-8");            StreamWriter sw = null;
            string str = SiteTemplate();//读取模版页面html代码             string htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";//静态文件名
            
            // 替换内容
            //str = str.Replace("ShowArticle", strText);//这句不知道干什么用的
            str = str.Replace("biaoti", strText);
            str = str.Replace("content", HttpUtility.HtmlDecode(strContent));
            str = str.Replace("author", strAuthor);
            // 写文件
            try
            {
                sw = new StreamWriter("E:Hover/htm/" + htmlfilename, false, code);
                sw.Write(str);
                sw.Flush();
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
                HttpContext.Current.Response.End();
            }
            finally
            {
                sw.Close();
            }
            return true;
        }用这段代码生成静态页面以后,打开HTML文件一看,段落格式都没了,怎么解决呀?顺便告诉我怎么结贴给分...汗

解决方案 »

  1.   

    至于html代码换行不换行都无所谓了吧 只要是打开结果是对的就ok了吧
    说下html代码换行的办法吧 "\n" 是换行
    "\t" 是缩进更多转义字符Character
     Escape Sequence
     
    '
     \'
     
    "
     \"
     
    \
     \\
     
    警报
     \a
     
    退格符
     \b
     
    换页符
     \f
     
    换行符
     \n
     
    回车符
     \r
     
    Tab 符
     \t
     
    垂直 Tab 符
     \v
     
    使用数字指定的Unicode 字符,如\u2000
     \u
     
    使用十六进制数指定的Unicode 字符,如\xc8
     \x
     
    空值
     \0 (zero)
     
      

  2.   

    谢谢,但是生成的HTML文件打开以后挤成一坨,让网页的浏览者怎么看啊?难道在更新网站内容的时候,输入一篇文章吧,要在换行的地方加上"\n",在缩进的地方加上"\t"吗?那维护的时候太不方便了吧?能提供一段例子?或者直接在我的代码上改...辛苦各位高手啦~~
      

  3.   

     //string path = HttpContext.Current.Server.MapPath(@".\htm\");
    一般我会这样写!
      

  4.   

    //string path = HttpContext.Current.Server.MapPath("/htm/");//文件输出目录,如果有这句还会出异常,是未能映射路径"/htm/" 
    这句是告诉你生成的静态页面放在哪儿,有错误是你的路径不对,或者文件夹没有创建,需要你创建文件夹。还有希望你的模板里有特殊字符。只有特殊字符替换的时候才不会有可能跟你的正文里的东西冲突。你这样问题是很多的。样式乱了你可以用编辑器打开你生成的页面看看,到底是css没有还是标记缺失了。自己仔细的看看就知道了。
      

  5.   

    我是这样做的,页面上有一个textbox(当然是多行的),把文章直接复制到里面,然后点一个按钮,就生成HTML文件了,根本涉及不到写标记的问题啊,CSS我倒没有,是不是要写一个CSS文件,然后创建HTML模板的时候引用这个CSS呢?
      

  6.   

    是不是只能这样做呢,我把有段落的文章复制倒dreamweaver里,然后点“代码”,把生成的HTML代码复制到TextBox里,再生成HTML文件,应该就有格式了吧?