C#.net生成html代码 有没有C#.net生成html代码的类啊,我想生成一个全静态的网站,除了后台 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 for(int N=1;N<=100;N++){MessageBox.Show("自顶“+N+”下")} 看看是否有用.http://www.th7.cn/Article/bc/nt/200704/20646_2.html 用System.IO往文件里写html代码就行了 public static bool WriteFile(string strText) { string path = HttpContext.Current.Server.MapPath("Html/"); System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312"); string temp = HttpContext.Current.Server.MapPath("Html/A.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"; //通过正则获取标签内容实现内容替换 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; } 这个我知道,只是如果是从数据库里读取数据再生成HTML的话要遍历数据库,那要怎样遍历呢,最好是C#代码 /// <summary> /// 创建html页面 /// </summary> protected void Create(string createHtmlTemplatePage, string htmlPage) //createHtmlTemplatePage:以哪个页面为模板生成html,htmlPage:生成的静态页的名字 { string sUrl = "", sHtmlContent = "", sHost = "", sPath = "", sCurPage = ""; sHost = Request.Url.Authority; sCurPage = Request.Url.Segments[Request.Url.Segments.Length - 1]; sPath = Request.Path.Replace(sCurPage, createHtmlTemplatePage); sUrl = "http://" + sHost + sPath; System.Net.WebRequest wReq = System.Net.WebRequest.Create(sUrl); System.Net.WebResponse wResp = wReq.GetResponse(); System.IO.Stream respStream = wResp.GetResponseStream(); System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("utf-8")); sHtmlContent = reader.ReadToEnd(); respStream.Dispose(); reader.Dispose(); if (File.Exists(Server.MapPath(htmlPage))) { File.Delete(htmlPage); } //写入文件 StreamWriter sw; sw = new StreamWriter(Server.MapPath(htmlPage), false, System.Text.Encoding.GetEncoding("utf-8")); sw.Write(sHtmlContent); sw.Dispose(); }封装之后直接调用 用自己html模板,然后读取,生成字符串,用流输出就行了 我都是用xx.replace("$html标记$","替换的内容")做的 不知道还有没有更好的实现? 重写Render方法,还需要类吗? http://blog.csdn.net/durongjian/archive/2010/01/26/5257208.aspx 这个回复不管用 是用来获取网页源代码的代码提醒一下(利用WebClient和WebRequest类获得网页源代码)呵呵 ASP.Net生成静态HTML页在Asp中实现的生成静态页用到的FileSystemObject对象!在.Net中涉及此类操作的是System.IO 以下是程序代码 注:此代码非原创!参考别人代码//生成HTML页 public static bool WriteFile(string strText,string strContent,string strAuthor) { string path = HttpContext.Current.Server.MapPath("/news/"); Encoding code = Encoding.GetEncoding("gb2312"); // 读取模板文件 string temp = HttpContext.Current.Server.MapPath("/news/text.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("ShowArticle",strText); //模板页中的ShowArticle str = str.Replace("biaoti",strText); str = str.Replace("content",strContent); str = str.Replace("author",strAuthor); // 写文件 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;此函数放在Conn.CS基类中了在添加新闻的代码中引用 注:工程名为Hover if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString))) { Response.Write("添加成功"); } else { Response.Write("生成HTML出错!"); }-------------------------------------------------------------------------模板页Text.html代码-------------------------------------------------------------------------<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML> <HEAD> <title>ShowArticle</title> <body> biaoti <br> content<br> author </body></HTML> protected void html() ////生成新闻静态 { //String Path = Server.MapPath(""); String txtPath = Server.MapPath(""); String ConnStr = ConfigurationManager.ConnectionStrings["CSDN"].ToString(); String StrSql = @"Select * From Content"; SqlDataAdapter DA = new SqlDataAdapter(StrSql, ConnStr); DataSet DS = new DataSet(); DA.Fill(DS,"Tables"); StreamReader sr = null; StreamWriter sw = null; Encoding code = Encoding.GetEncoding("gb2312"); try { String str = ""; sr = new StreamReader(txtPath + @"\temp_xinwen.html", code); str = sr.ReadToEnd(); foreach (DataRow dr in DS.Tables[0].Rows) { String tempStr = str; tempStr = tempStr.Replace("{$tape}", dr["title"].ToString()); tempStr = tempStr.Replace("{$title}", dr["title"].ToString()); tempStr = tempStr.Replace("{$content}",dr["content"].ToString()); tempStr = tempStr.Replace("{$1}", dr["title"].ToString()); tempStr = tempStr.Replace("{$url}", dr["down"].ToString()); try { sw = new StreamWriter(txtPath +@"\html_xinwen" +"\\" + dr["id"].ToString() + ".Html", false, code); sw.Write(tempStr); sw.Flush(); } catch (Exception ex) { if (sw != null) { sw.Close(); } } } Response.Write("<script>alert('已经生成N个静态新闻页面');</script>"); } catch { if (sr != null) sr.Close(); }} 你看一下http://www.csharp360.com/bbs/viewthread.php?tid=84&extra=page%3D1可能对你有用 救命啊!!!!!!!!!!! 学习压力测试 关于WF的App.Config文件的使用 vs2005中那个修改默认布局的地方在哪? 各位大侠帮帮忙,没分了,gridview中我如何根据数据库中的值在列表中显示不同的图片 asp.net web 开发 连接Access数据库无法增加记录 关与.net程序发布问题 java程序和。net的程序怎么连接在一起亚,session部能共享 请问在asp.net下如何实现批量录入 在asp.net添加引用OCX控件,为何在工具箱上看不见 ? 寻求js日历控件?
for(int N=1;N<=100;N++)
{
MessageBox.Show("自顶“+N+”下")
}
http://www.th7.cn/Article/bc/nt/200704/20646_2.html
{
string path = HttpContext.Current.Server.MapPath("Html/");
System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");
string temp = HttpContext.Current.Server.MapPath("Html/A.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";
//通过正则获取标签内容实现内容替换
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;
}
这个我知道,只是如果是从数据库里读取数据再生成HTML的话要遍历数据库,那要怎样遍历呢,最好是C#代码
/// 创建html页面
/// </summary>
protected void Create(string createHtmlTemplatePage, string htmlPage) //createHtmlTemplatePage:以哪个页面为模板生成html,htmlPage:生成的静态页的名字
{
string sUrl = "", sHtmlContent = "", sHost = "", sPath = "", sCurPage = ""; sHost = Request.Url.Authority;
sCurPage = Request.Url.Segments[Request.Url.Segments.Length - 1];
sPath = Request.Path.Replace(sCurPage, createHtmlTemplatePage);
sUrl = "http://" + sHost + sPath; System.Net.WebRequest wReq = System.Net.WebRequest.Create(sUrl);
System.Net.WebResponse wResp = wReq.GetResponse();
System.IO.Stream respStream = wResp.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("utf-8")); sHtmlContent = reader.ReadToEnd();
respStream.Dispose();
reader.Dispose();
if (File.Exists(Server.MapPath(htmlPage)))
{
File.Delete(htmlPage);
}
//写入文件
StreamWriter sw;
sw = new StreamWriter(Server.MapPath(htmlPage), false, System.Text.Encoding.GetEncoding("utf-8"));
sw.Write(sHtmlContent);
sw.Dispose();
}
封装之后直接调用
呵呵
在Asp中实现的生成静态页用到的FileSystemObject对象!
在.Net中涉及此类操作的是System.IO
以下是程序代码 注:此代码非原创!参考别人代码
//生成HTML页
public static bool WriteFile(string strText,string strContent,string strAuthor)
{
string path = HttpContext.Current.Server.MapPath("/news/");
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("/news/text.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("ShowArticle",strText); //模板页中的ShowArticle
str = str.Replace("biaoti",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 写文件
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;此函数放在Conn.CS基类中了
在添加新闻的代码中引用 注:工程名为Hover
if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出错!");
}
-------------------------------------------------------------------------
模板页Text.html代码
-------------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ShowArticle</title>
<body> biaoti
<br>
content<br>
author
</body>
</HTML>
{
//String Path = Server.MapPath(""); String txtPath = Server.MapPath(""); String ConnStr = ConfigurationManager.ConnectionStrings["CSDN"].ToString(); String StrSql = @"Select * From Content"; SqlDataAdapter DA = new SqlDataAdapter(StrSql, ConnStr); DataSet DS = new DataSet(); DA.Fill(DS,"Tables"); StreamReader sr = null;
StreamWriter sw = null;
Encoding code = Encoding.GetEncoding("gb2312"); try
{
String str = "";
sr = new StreamReader(txtPath + @"\temp_xinwen.html", code); str = sr.ReadToEnd(); foreach (DataRow dr in DS.Tables[0].Rows)
{
String tempStr = str; tempStr = tempStr.Replace("{$tape}", dr["title"].ToString());
tempStr = tempStr.Replace("{$title}", dr["title"].ToString());
tempStr = tempStr.Replace("{$content}",dr["content"].ToString());
tempStr = tempStr.Replace("{$1}", dr["title"].ToString());
tempStr = tempStr.Replace("{$url}", dr["down"].ToString());
try
{
sw = new StreamWriter(txtPath +@"\html_xinwen" +"\\" + dr["id"].ToString() + ".Html", false, code);
sw.Write(tempStr);
sw.Flush(); }
catch (Exception ex)
{
if (sw != null)
{
sw.Close();
}
} } Response.Write("<script>alert('已经生成N个静态新闻页面');</script>"); }
catch
{
if (sr != null)
sr.Close(); }}
可能对你有用