C#生成HTML页类

来源:互联网 发布:电脑离线翻译软件 编辑:程序博客网 时间:2024/05/23 00:06
 /// <summary>
    
/// 生成HTML页类
    
/// </summary>
    public class ProduceHtmlPage
    {
        
public static bool WriteFile(string strTopic,string strArticleClass,string strArticleClassChild,string strContent,string strSource,string strAuthor,string strEditor,string strPubDate,string strPageUrl) 
        {
            
string path = HttpContext.Current.Server.MapPath("/bluedn/html/");
            Encoding code 
= Encoding.GetEncoding("gb2312");
            
//读取模板文件
            string temp = HttpContext.Current.Server.MapPath("/bluedn/template/NewsInfo.htm");
            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= strPageUrl+".htm";
            
//DateTime.Now.ToString("yyyyMMddHHmmss")+".htm";
            
//替换内容
            
//这时,模板文件已经读入到名称为str的变量中了

            
//模板页中的新闻标题
            str = str.Replace("$t_Topic$",strTopic); 
            
//一级栏目
            str = str.Replace("$t_ArticleClass$",strArticleClass); 
            
//二级栏目
            str = str.Replace("$t_ArticleClassChild$",strArticleClassChild); 
            
//新闻来源
            str = str.Replace("$t_Source$",strSource);
            
//作者
            str = str.Replace("$t_Author$",strAuthor);
            
//编辑
            str = str.Replace("$t_Editor$",strEditor);
            
//发布日期
            str = str.Replace("$t_PubDate$",strPubDate);
            
//新闻内容
            str = str.Replace("$t_Content$",strContent);

            
//写文件
            try
            {
                sw 
= new StreamWriter(path + 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;
        }
    }
 
原创粉丝点击