这是全部的生成静态列表的代码
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Data;
using System.Web;
using System.Text.RegularExpressions;
using ROYcms.Common;
namespace ROYcms.Templet
{
public class NewList
{
Template Template = new Template();
ROYcms.Sys.Bll.ROYcms_news BLL = new ROYcms.Sys.Bll.ROYcms_news();
///// <summary>
///// 循环标签的匹配替换
///// </summary>
///// <param name="template">模板内容</param>
public void NewHtml()
{
//定义参数
StreamWriter sw = null;
string htmlfilename;
string path = HttpContext.Current.Server.MapPath("html/");
string templatepath = "1.html";
string HTML = SystemCms.Read_File(HttpContext.Current.Server.MapPath(templatepath));
// 数据库生成分页
int onepage = 10;
int Count = new ROYcms.Sys.Bll.ROYcms_news().GetCount("");
int allpages = Count / onepage;
string Text = "";
string AllText = "";
string content = "";
Regex r = new Regex(@"(\[SG:loop\s+(?<attributes>[^\]]*?)\](?<text>[\s\S]*?)\[/SG:loop\])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
for (int i = 0; i < allpages; i++)
{
htmlfilename = "index_" + "" + i + "" + ".html";
int start = i * onepage;
int nextpage = i + 1;
int perpage = i - 1;
if (nextpage == allpages)
{
nextpage = i;
}
if (perpage == -1)
{
perpage = 0;
}
DataSet ds = BLL.GetDataSet("SELECT TOP " + onepage + " * FROM [ROYcms_news] WHERE (bh NOT IN (SELECT TOP " + start + " bh FROM [ROYcms_news] ORDER BY bh )) ORDER BY bh ");
DataTable dt = ds.Tables[0];
sw = new StreamWriter(path + htmlfilename, false, Encoding.GetEncoding("GB2312"));
#region 对循环的内容进行替换
foreach (Match m in r.Matches(HTML))
{
Text = m.Groups["text"].ToString(); //循环的内容不包含SG:Loop
AllText = m.Groups[0].Value.ToString(); //整个匹配的内容包含SG:Loop
foreach (DataRow dr in dt.Rows)
{
string str = Text;
str = Template.Replace(str, @"\[SG:Id\]", dr["bh"].ToString());
str = Template.Replace(str, @"\[SG:Title\]", dr["title"].ToString());
str = Template.Replace(str, @"\[SG:zhaiyao\]", dr["zhaiyao"].ToString());
str = Template.Replace(str, @"\[SG:pic\]", dr["pic"].ToString());
str = Template.Replace(str, @"\[SG:classname\]", dr["classname"].ToString());
str = Template.Replace(str, @"\[SG:author\]", dr["author"].ToString());
str = Template.Replace(str, @"\[SG:time\]", dr["time"].ToString());
str = Template.Replace(str, @"\[SG:Year\]", Convert.ToDateTime(dr["time"]).ToString("yyyy"));
str = Template.Replace(str, @"\[SG:Month\]", Convert.ToDateTime(dr["time"]).ToString("MM"));
str = Template.Replace(str, @"\[SG:Day\]", Convert.ToDateTime(dr["time"]).ToString("dd"));
str = Template.Replace(str, @"\[SG:Link\]", Convert.ToDateTime(dr["time"]).ToString("yyyy") + "/" + Convert.ToDateTime(dr["time"]).ToString("MM") + "/" + Convert.ToDateTime(dr["time"]).ToString("dd") + "/" + dr["bh"].ToString()+".html");
content += AllText.Replace(AllText, str);
}
HTML = HTML.Replace(AllText, content);
HTML = HTML.Replace(@"[SG:PageUp]", (perpage + 1).ToString());
HTML = HTML.Replace(@"[SG:PageDow]", (nextpage + 1).ToString());
content = "";
}
#endregion
//创建文件
try
{
sw.WriteLine(HTML);
sw.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally { sw.Close(); }
}
}
}
}
这是1.html 模版标签文件
Code
<!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>
</head>
<body>
===========================================================================
[SG:loop NewsCount="40"]
<a href=[SG:Link]>[SG:Title]</a>
<br />
[/SG:loop]
<a href=index_[SG:PageUp].html>[SG:PageUp]</a>
<a href=index_[SG:PageDow].html>[SG:PageDow]</a>
=====================================================================
----------------------------------------------------------------
===========================================================================
[SG:loop NewsCount="4"]
[SG:Title]
[/SG:loop]
=====================================================================
----------------------------------------------------------------
[SG:loop NewsCount="4"]
[SG:Year]-[SG:Title]
<hr />
[/SG:loop]
[SG:Title]
<hr />
[SG:Content]
---------------------------------------------------------------
</body>
</html>
问题是生成了分页文件从 index_0.html 到index_22.html
为什么列表内容都是一样的 如下表
从 index_0.html 到index_22.html 生成德内容都是第一页 全部是下面的内容
大家帮我看看那里逻辑错误 感谢
Code
<!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>
</head>
<body>
===========================================================================
<a href=2009/08/11/5.html>ww</a>
<br />
<a href=2009/08/13/6.html>women</a>
<br />
<a href=2009/08/13/7.html>123</a>
<br />
<a href=2009/08/13/8.html>wqe</a>
<br />
<a href=2009/08/14/9.html>企业信息测试</a>
<br />
<a href=2009/08/14/10.html>郎子文化公司 信息测试</a>
<br />
<a href=2009/08/16/13.html>waeaw</a>
<br />
<a href=2009/08/16/14.html>金地集团通过挂牌方式以6.85亿元获得西安曲江新区住宅地块</a>
<br />
<a href=2009/08/16/15.html>陕西凤翔儿童血铅超标</a>
<br />
<a href=2009/08/16/16.html>通城县电信局大楼7层楼顶十几米高的铁塔轰然倒塌</a>
<br />
<a href=index_1.html>1</a>
<a href=index_2.html>2</a>
=====================================================================
----------------------------------------------------------------
===========================================================================
ww
women
123
wqe
企业信息测试
郎子文化公司 信息测试
waeaw
金地集团通过挂牌方式以6.85亿元获得西安曲江新区住宅地块
陕西凤翔儿童血铅超标
通城县电信局大楼7层楼顶十几米高的铁塔轰然倒塌
=====================================================================
----------------------------------------------------------------
2009-ww
<hr />
2009-women
<hr />
2009-123
<hr />
2009-wqe
<hr />
2009-企业信息测试
<hr />
2009-郎子文化公司 信息测试
<hr />
2009-waeaw
<hr />
2009-金地集团通过挂牌方式以6.85亿元获得西安曲江新区住宅地块
<hr />
2009-陕西凤翔儿童血铅超标
<hr />
2009-通城县电信局大楼7层楼顶十几米高的铁塔轰然倒塌
<hr />
[SG:Title]
<hr />
[SG:Content]
---------------------------------------------------------------
</body>
</html>