//
------------------------------------调用方法-------------------------------------
// 所要替换的内容如title 、content 、author 、time 等可以从数据库或者页面用户输入来获得
string title = " 新闻标题 " ;
string content = " 新闻内容 " ;
string author = " 新闻作者 " ;
string time = DateTime.Now.ToString(); // 发布时间
string [][] replaceSign = new string [][]
... {
new string[]...{"$title$",title},
new string[]...{"$content$",content},
new string[]...{"$author$",author},
new string[]...{"$time$",time}
} ;
string templatePage = " Model/newsModel.htm " ;
string saveHtmlPage = " News/ " + DateTime.Now.ToString( " yyyyMMddHHmmss " ) + " .htm " ;
string encodingName = " GB2312 " ;
DHtml.WriteHtml(replaceSign, templatePage, saveHtmlPage, encodingName);
// 所要替换的内容如title 、content 、author 、time 等可以从数据库或者页面用户输入来获得
string title = " 新闻标题 " ;
string content = " 新闻内容 " ;
string author = " 新闻作者 " ;
string time = DateTime.Now.ToString(); // 发布时间
string [][] replaceSign = new string [][]
... {
new string[]...{"$title$",title},
new string[]...{"$content$",content},
new string[]...{"$author$",author},
new string[]...{"$time$",time}
} ;
string templatePage = " Model/newsModel.htm " ;
string saveHtmlPage = " News/ " + DateTime.Now.ToString( " yyyyMMddHHmmss " ) + " .htm " ;
string encodingName = " GB2312 " ;
DHtml.WriteHtml(replaceSign, templatePage, saveHtmlPage, encodingName);
//
-----------------------根据模版生成静态页面---------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/**/ /// <summary>
/// DHtml 的摘要说明
/// </summary>
public class DHtml
... {
public DHtml()
...{
}
/**//// <summary>
/// 动态生成HTML页
/// </summary>
/// <param name="replaceSign">用于替换HTML模板的变量数组</param>
/// <param name="templatePage">所要调用的HTML模板页面名称</param>
/// <param name="saveHtmlPage">保存所生成的静态HTML页面名称</param>
/// <param name="encodingName">读取HTML模板页和生成HTML页所使用的字符编码方式(如:"GB2312")</param>
/// <returns>bool类型(true=生成成功,false=生成失败)</returns>
public static bool WriteHtml(string[][] replaceSign, string templatePage, string saveHtmlPage, string encodingName)
...{
System.Web.UI.Page page = new Page();
//---------------------读html模板页面到stringbuilder对象里----
System.Text.StringBuilder htmltext = new System.Text.StringBuilder();
try
...{
System.IO.StreamReader sr = new System.IO.StreamReader(page.Server.MapPath(templatePage.Trim()), System.Text.Encoding.GetEncoding(encodingName));
string line;
while ((line = sr.ReadLine()) != null)
...{
htmltext.Append(line);
}
sr.Close();
}
catch
...{
System.Web.HttpContext.Current.Response.Write("<script>alert('读取HTML模板 " + templatePage.Trim() + " 出错!');</script>");
}
//----------替换htm模板里的标记为传入的数组
for (int i = 0; i < replaceSign.Length; i++)
...{
htmltext.Replace(replaceSign[i][0], replaceSign[i][1]);
}
//----------生成htm文件------------------――
try
...{
string tmphtm = page.Server.MapPath(saveHtmlPage);
System.IO.StreamWriter sw = new System.IO.StreamWriter(tmphtm.Trim(), false, System.Text.Encoding.GetEncoding(encodingName));
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
return true;
}
catch
...{
System.Web.HttpContext.Current.Response.Write("<script>alert('" + saveHtmlPage.Trim() + " HTML文件生成失败!');</script>");
return false;
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/**/ /// <summary>
/// DHtml 的摘要说明
/// </summary>
public class DHtml
... {
public DHtml()
...{
}
/**//// <summary>
/// 动态生成HTML页
/// </summary>
/// <param name="replaceSign">用于替换HTML模板的变量数组</param>
/// <param name="templatePage">所要调用的HTML模板页面名称</param>
/// <param name="saveHtmlPage">保存所生成的静态HTML页面名称</param>
/// <param name="encodingName">读取HTML模板页和生成HTML页所使用的字符编码方式(如:"GB2312")</param>
/// <returns>bool类型(true=生成成功,false=生成失败)</returns>
public static bool WriteHtml(string[][] replaceSign, string templatePage, string saveHtmlPage, string encodingName)
...{
System.Web.UI.Page page = new Page();
//---------------------读html模板页面到stringbuilder对象里----
System.Text.StringBuilder htmltext = new System.Text.StringBuilder();
try
...{
System.IO.StreamReader sr = new System.IO.StreamReader(page.Server.MapPath(templatePage.Trim()), System.Text.Encoding.GetEncoding(encodingName));
string line;
while ((line = sr.ReadLine()) != null)
...{
htmltext.Append(line);
}
sr.Close();
}
catch
...{
System.Web.HttpContext.Current.Response.Write("<script>alert('读取HTML模板 " + templatePage.Trim() + " 出错!');</script>");
}
//----------替换htm模板里的标记为传入的数组
for (int i = 0; i < replaceSign.Length; i++)
...{
htmltext.Replace(replaceSign[i][0], replaceSign[i][1]);
}
//----------生成htm文件------------------――
try
...{
string tmphtm = page.Server.MapPath(saveHtmlPage);
System.IO.StreamWriter sw = new System.IO.StreamWriter(tmphtm.Trim(), false, System.Text.Encoding.GetEncoding(encodingName));
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
return true;
}
catch
...{
System.Web.HttpContext.Current.Response.Write("<script>alert('" + saveHtmlPage.Trim() + " HTML文件生成失败!');</script>");
return false;
}
}
}
//
----------------------------------附模版文件NewsModel.htm的代码-------------------------------
<! 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 >
< title > $title$ </ title >
</ head >
< body >
< table width = " 750 " align = " center " >
< tr >
< td valign = " top " >
< div >
< div >
< h2 align = " center " style = " padding-bottom:5px; padding-top:5px " >
< span > $title$ </ span >
</ h2 >
</ div >
< div >
< h3 align = " center " style = " padding-bottom:3px; padding-top:3px " >
发布时间: < span > $time$ </ span >& nbsp
发布人: < span > $author$ </ span >
</ h3 >
</ div >
< div >
< span > $content$ </ span >
</ div >
</ div >
</ td >
</ tr >
</ table >
</ body >
</ 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 >
< title > $title$ </ title >
</ head >
< body >
< table width = " 750 " align = " center " >
< tr >
< td valign = " top " >
< div >
< div >
< h2 align = " center " style = " padding-bottom:5px; padding-top:5px " >
< span > $title$ </ span >
</ h2 >
</ div >
< div >
< h3 align = " center " style = " padding-bottom:3px; padding-top:3px " >
发布时间: < span > $time$ </ span >& nbsp
发布人: < span > $author$ </ span >
</ h3 >
</ div >
< div >
< span > $content$ </ span >
</ div >
</ div >
</ td >
</ tr >
</ table >
</ body >
</ html >