RSS订阅的实例
1.新建一用户控件RSSCommonListModule.ascx,读取RSS数据
ASPX:
CS:
2.新建Web窗体rss_news.aspx,将用户控件拖入,添加RSS地址
3.主页上插入个IFRAME,链接至rss_news.aspx
ASPX:
1 <table width="100%" id="tb<%=this.ClientID%>" style="display:none">
2 <tr>
3 <td width="50%">
4 <ul class="ul_list2">
5 <asp:repeater id="rpList" Runat="server">
6 <ItemTemplate>
7 <li>
8 <span><%# DataBinder.Eval(Container.DataItem,"pubDate","{0:d}")%><asp:Literal ID="litNewImg" Runat="server" Visible="False"></asp:Literal></span>
9 <A href='<%# DataBinder.Eval(Container.DataItem,"link")%>' target="_blank" title="<%# DataBinder.Eval(Container.DataItem,"title").ToString()%>">
10 <%# GetStringByLen(DataBinder.Eval(Container.DataItem,"title").ToString(),19)%>
11
12 </A>
13
14
15 <asp:Literal ID="litPubDate" Runat="server" Visible="False" Text='<%# DataBinder.Eval(Container.DataItem,"pubDate","{0:d}")%>'></asp:Literal>
16 <asp:Literal ID="litTD" Runat="server"></asp:Literal>
17 </li>
18 </ItemTemplate>
19 </asp:repeater>
20 </ul>
21 </td>
22 </tr>
23 </table>
2 <tr>
3 <td width="50%">
4 <ul class="ul_list2">
5 <asp:repeater id="rpList" Runat="server">
6 <ItemTemplate>
7 <li>
8 <span><%# DataBinder.Eval(Container.DataItem,"pubDate","{0:d}")%><asp:Literal ID="litNewImg" Runat="server" Visible="False"></asp:Literal></span>
9 <A href='<%# DataBinder.Eval(Container.DataItem,"link")%>' target="_blank" title="<%# DataBinder.Eval(Container.DataItem,"title").ToString()%>">
10 <%# GetStringByLen(DataBinder.Eval(Container.DataItem,"title").ToString(),19)%>
11
12 </A>
13
14
15 <asp:Literal ID="litPubDate" Runat="server" Visible="False" Text='<%# DataBinder.Eval(Container.DataItem,"pubDate","{0:d}")%>'></asp:Literal>
16 <asp:Literal ID="litTD" Runat="server"></asp:Literal>
17 </li>
18 </ItemTemplate>
19 </asp:repeater>
20 </ul>
21 </td>
22 </tr>
23 </table>
CS:
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using NRss;
using System.Text;
using System.IO;
public class RssCache
{
public DateTime lastFlashDT;
public Rss rss;
public RssChannelItemCollection showdata;
}
/// <summary>
/// RSSCommonListModule 的摘要说明。
/// </summary>
public class RSSCommonListModule : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Repeater rpList;
private string _cssclass;
private string _morelink;
public string Cssclass
{
get{return _cssclass; }
set{_cssclass=value;}
}
public string MoreLink
{
get{return _morelink; }
set{_morelink=value;}
}
public string RssAddress
{
get
{
return ViewState["__RssAddress__"]==null?"":ViewState["__RssAddress__"].ToString();
}
set
{
ViewState["__RssAddress__"] =value;
}
}
public string RssTitle
{
get
{
return ViewState["__RssTitle__"]==null?"":ViewState["__RssTitle__"].ToString();
}
set
{
ViewState["__RssTitle__"] =value;
}
}
private int _contentcnt = 0;
public int ContentCnt
{
get
{
return _contentcnt;
}
set
{
_contentcnt = value;
}
}
private int _maxcontentcnt = 0;
public int MaxContentCnt
{
get
{
return _maxcontentcnt;
}
set
{
_maxcontentcnt = value;
}
}
private int _reflashtime = 10;
public int ReflashTime
{
get
{
return _reflashtime;
}
set
{
_reflashtime = value;
}
}
public RssCache RssData
{
get
{
return Cache["rssdata_"+this.ClientID]==null?null:(Cache["rssdata_"+this.ClientID] as RssCache);
}
set
{
Cache["rssdata_"+this.ClientID] = value;
}
}
protected string AppPath
{
get
{
return "";
}
}
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
this.BindList();
}
}
private void BindList()
{
RssReader reader;
//Rss rss;
if(RssAddress==null||RssAddress=="")
return;
reader = new RssReader(RssAddress);
try
{
if(this.RssData==null)
{
this.RssData = new RssCache();
this.RssData.rss = reader.Read();
this.RssData.lastFlashDT = DateTime.Now;
int itemCnt = MaxContentCnt;
ContentCnt = this.RssData.rss.Channel.Items.Count;
if(MaxContentCnt>ContentCnt)
itemCnt = ContentCnt;
this.RssTitle = this.RssData.rss.Channel.Title.Trim();
RssChannelItemCollection data = new RssChannelItemCollection();
for(int i=0;i<itemCnt;i++)
{
data.Add(this.RssData.rss.Channel.Items[i]);
}
this.RssData.showdata = data;
}
else
{
DateTime now = DateTime.Now;
DateTime Last = this.RssData.lastFlashDT.AddMinutes(this.ReflashTime);
//如果超过了设定时间则重新加载
if(now>Last)
{
this.RssData.rss = reader.Read();
this.RssData.lastFlashDT = DateTime.Now;
int itemCnt = MaxContentCnt;
ContentCnt = this.RssData.rss.Channel.Items.Count;
if(MaxContentCnt>ContentCnt)
itemCnt = ContentCnt;
this.RssTitle = this.RssData.rss.Channel.Title.Trim();
RssChannelItemCollection data = new RssChannelItemCollection();
for(int i=0;i<itemCnt;i++)
{
data.Add(this.RssData.rss.Channel.Items[i]);
}
this.RssData.showdata = data;
}
}
ContentCnt = this.RssData.rss.Channel.Items.Count;
if(this.RssData.showdata!=null)
{
this.rpList.DataSource =this.RssData.showdata;
this.rpList.DataBind();
}
}
catch(Exception ex)
{
reader.Close();
}
finally
{
reader.Close();
}
//
// DataSet ds = new DataSet();
// StreamReader srReadGbkXml = new StreamReader(RssAddress, System.Text.Encoding.GetEncoding("gb2312"));
//
// ds.ReadXml(srReadGbkXml);
//
//
// this.rpList.DataSource = ds.Tables["item"];
// this.rpList.DataBind();
}
private void rpList_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
Literal litNewImg= (Literal) e.Item.FindControl("litNewImg");
Literal litTD= (Literal) e.Item.FindControl("litTD");
Literal litPubDate= (Literal) e.Item.FindControl("litPubDate");
DateTime dt1=DateTime.Now;
// if(drv["CreateDt"].ToString().Trim().Substring(0,10)==dt.ToString().Trim().Substring(0,10))
// litNew.Text="<font color='red'>New</font>";
DateTime dt=DateTime.Parse(litPubDate.Text.ToString().Trim());
TimeSpan ts=dt1-dt;
int day=ts.Days;
if(day<Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["ContentIsNewLimitDay"]))
litNewImg.Text="<img src='"+this.AppPath+"Images/new3.gif'>";
int m = ContentCnt/2;
if((ContentCnt%2)!=0)
m = (ContentCnt+1)/2;
if(m>10)
m=10;
if((e.Item.ItemIndex+1)==m)
{
litTD.Text="</ul></td><td><ul class=\"ul_list2\">";
}
}
public string GetStringByLen(string str,int len)
{
if(str.Length>len)
str=str.Substring(0,len);
return str;
}
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using NRss;
using System.Text;
using System.IO;
public class RssCache
{
public DateTime lastFlashDT;
public Rss rss;
public RssChannelItemCollection showdata;
}
/// <summary>
/// RSSCommonListModule 的摘要说明。
/// </summary>
public class RSSCommonListModule : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Repeater rpList;
private string _cssclass;
private string _morelink;
public string Cssclass
{
get{return _cssclass; }
set{_cssclass=value;}
}
public string MoreLink
{
get{return _morelink; }
set{_morelink=value;}
}
public string RssAddress
{
get
{
return ViewState["__RssAddress__"]==null?"":ViewState["__RssAddress__"].ToString();
}
set
{
ViewState["__RssAddress__"] =value;
}
}
public string RssTitle
{
get
{
return ViewState["__RssTitle__"]==null?"":ViewState["__RssTitle__"].ToString();
}
set
{
ViewState["__RssTitle__"] =value;
}
}
private int _contentcnt = 0;
public int ContentCnt
{
get
{
return _contentcnt;
}
set
{
_contentcnt = value;
}
}
private int _maxcontentcnt = 0;
public int MaxContentCnt
{
get
{
return _maxcontentcnt;
}
set
{
_maxcontentcnt = value;
}
}
private int _reflashtime = 10;
public int ReflashTime
{
get
{
return _reflashtime;
}
set
{
_reflashtime = value;
}
}
public RssCache RssData
{
get
{
return Cache["rssdata_"+this.ClientID]==null?null:(Cache["rssdata_"+this.ClientID] as RssCache);
}
set
{
Cache["rssdata_"+this.ClientID] = value;
}
}
protected string AppPath
{
get
{
return "";
}
}
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
this.BindList();
}
}
private void BindList()
{
RssReader reader;
//Rss rss;
if(RssAddress==null||RssAddress=="")
return;
reader = new RssReader(RssAddress);
try
{
if(this.RssData==null)
{
this.RssData = new RssCache();
this.RssData.rss = reader.Read();
this.RssData.lastFlashDT = DateTime.Now;
int itemCnt = MaxContentCnt;
ContentCnt = this.RssData.rss.Channel.Items.Count;
if(MaxContentCnt>ContentCnt)
itemCnt = ContentCnt;
this.RssTitle = this.RssData.rss.Channel.Title.Trim();
RssChannelItemCollection data = new RssChannelItemCollection();
for(int i=0;i<itemCnt;i++)
{
data.Add(this.RssData.rss.Channel.Items[i]);
}
this.RssData.showdata = data;
}
else
{
DateTime now = DateTime.Now;
DateTime Last = this.RssData.lastFlashDT.AddMinutes(this.ReflashTime);
//如果超过了设定时间则重新加载
if(now>Last)
{
this.RssData.rss = reader.Read();
this.RssData.lastFlashDT = DateTime.Now;
int itemCnt = MaxContentCnt;
ContentCnt = this.RssData.rss.Channel.Items.Count;
if(MaxContentCnt>ContentCnt)
itemCnt = ContentCnt;
this.RssTitle = this.RssData.rss.Channel.Title.Trim();
RssChannelItemCollection data = new RssChannelItemCollection();
for(int i=0;i<itemCnt;i++)
{
data.Add(this.RssData.rss.Channel.Items[i]);
}
this.RssData.showdata = data;
}
}
ContentCnt = this.RssData.rss.Channel.Items.Count;
if(this.RssData.showdata!=null)
{
this.rpList.DataSource =this.RssData.showdata;
this.rpList.DataBind();
}
}
catch(Exception ex)
{
reader.Close();
}
finally
{
reader.Close();
}
//
// DataSet ds = new DataSet();
// StreamReader srReadGbkXml = new StreamReader(RssAddress, System.Text.Encoding.GetEncoding("gb2312"));
//
// ds.ReadXml(srReadGbkXml);
//
//
// this.rpList.DataSource = ds.Tables["item"];
// this.rpList.DataBind();
}
private void rpList_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
Literal litNewImg= (Literal) e.Item.FindControl("litNewImg");
Literal litTD= (Literal) e.Item.FindControl("litTD");
Literal litPubDate= (Literal) e.Item.FindControl("litPubDate");
DateTime dt1=DateTime.Now;
// if(drv["CreateDt"].ToString().Trim().Substring(0,10)==dt.ToString().Trim().Substring(0,10))
// litNew.Text="<font color='red'>New</font>";
DateTime dt=DateTime.Parse(litPubDate.Text.ToString().Trim());
TimeSpan ts=dt1-dt;
int day=ts.Days;
if(day<Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["ContentIsNewLimitDay"]))
litNewImg.Text="<img src='"+this.AppPath+"Images/new3.gif'>";
int m = ContentCnt/2;
if((ContentCnt%2)!=0)
m = (ContentCnt+1)/2;
if(m>10)
m=10;
if((e.Item.ItemIndex+1)==m)
{
litTD.Text="</ul></td><td><ul class=\"ul_list2\">";
}
}
public string GetStringByLen(string str,int len)
{
if(str.Length>len)
str=str.Substring(0,len);
return str;
}
2.新建Web窗体rss_news.aspx,将用户控件拖入,添加RSS地址
<uc1:RSSCommonListModule id="RSSCommonListModule1" runat="server" RssAddress="http://news.google.cn/nwshp?oe=UTF-8&tab=wn&ned=ccn&output=rss"
MoreLink="#" MaxContentCnt="20" ReflashTime="30"></uc1:RSSCommonListModule>
MoreLink="#" MaxContentCnt="20" ReflashTime="30"></uc1:RSSCommonListModule>
3.主页上插入个IFRAME,链接至rss_news.aspx
<div id="div_rss_news1_msg" align="center"><font color="red">新闻数据加载中请稍候!</font></div>
<iframe src="rss_news1.aspx" frameborder="0" width="750" align="middle" id="news1" onreadystatechange="hideMsg('div_rss_news1_msg')">
</iframe>
<iframe src="rss_news1.aspx" frameborder="0" width="750" align="middle" id="news1" onreadystatechange="hideMsg('div_rss_news1_msg')">
</iframe>