rss订阅程序实现
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;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string Tablename = "Tb_Test";
string fname = System.Web.HttpContext.Current.Server.MapPath("RSS_Folder") + "\\" + Tablename + ".XML";
GetRss.WriteRss(fname, Tablename);
}
}
生成xml类页面
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;
using System.IO;
public class GetRss
{
/**//// <summary>
/// 根据文件路径写RSS文件
/// </summary>
/// <remarks>
/// 例如:
/// WriteRss("D:Vs2005GenerateRssRSS_Folder est_tb.xml","test_tb")
/// 注意:根据表的结构,需要调整生成的RSS源。本实例仅供测试用。
/// </remarks>
/// <param name="pathfilename">文件路径</param>
/// <param name="tablename">表名</param>
/// <returns>true or false</returns>
public static bool WriteRss(string pathfilename,string tablename)
{
try
{
FileInfo finfo = new FileInfo(pathfilename);
using (FileStream fs = finfo.OpenWrite())
{
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("UTF-8"));
sw.WriteLine(GetRss.GetRSSString(tablename));
sw.Flush();
sw.Close();
}
return true;
}
catch (System.Exception ex)
{
System.Web.HttpContext.Current.Response.Write(ex.Message);
return false;
throw;
}
}
/**//// <summary>
/// 组织符合最新标准的RSS字符串
/// 参数:表名。
/// </summary>
/// <remarks>
/// 例如:
/// GetRSS()
/// 注意:根据表的结构,需要调整生成的RSS源。本实例仅供测试用。
/// </remarks>
/// <param name="tablename">表名</param>
/// <returns>返回一个DataSet 数据源</returns>
public static string GetRSSString(string Tablename)
{
try
{
DataSet ds = GetRSSData(Tablename);
string strRSS = "";
strRSS = strRSS + "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + System.Environment.NewLine;
strRSS = strRSS + " <rss version=\"2.0\" xmlns:dc=\"http://purl.org/dc/elements/1.1//" xmlns:trackback=\"http://madskills.com/public/xml/rss/module/trackback//" xmlns:wfw=\"http://wellformedweb.org/CommentAPI//" xmlns:slash=\"http://purl.org/rss/1.0/modules/slash//">" + System.Environment.NewLine;
strRSS = strRSS + " <channel xmlns:cfi=\"http://www.microsoft.com/schemas/rss/core/2005/internal/" cfi:lastdownloaderror=\"None\">" + System.Environment.NewLine;
strRSS = strRSS + " <title>订阅标题</title> " + System.Environment.NewLine;
strRSS = strRSS + " <link>http://www.**com.cn/</link>" + System.Environment.NewLine;
strRSS = strRSS + " <description>描述信息</description>" + System.Environment.NewLine;
strRSS = strRSS + " <language>zh-CN</language>" + System.Environment.NewLine;
strRSS = strRSS + " <generator>www.**com.cn</generator> " + System.Environment.NewLine;
strRSS = strRSS + " <copyright>北京**公司</copyright> " + System.Environment.NewLine;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
strRSS = strRSS + " <item>" + System.Environment.NewLine;
strRSS = strRSS + " <title>" + ds.Tables[0].Rows[i]["bt"] + "</title> " + System.Environment.NewLine;
strRSS = strRSS + " <link>http://www.**.com.cn/ArticleShow@" + ds.Tables[0].Rows[i]["ID"] + ".html</link> " + System.Environment.NewLine;
strRSS = strRSS + " <author /> " + System.Environment.NewLine;
strRSS = strRSS + " <guid>http://www.**.com.cn/ArticleShow@" + ds.Tables[0].Rows[i]["ID"] + ".html</guid> " + System.Environment.NewLine;
strRSS = strRSS + " <pubDate>" + Convert.ToDateTime(ds.Tables[0].Rows[i]["sj"].ToString()).ToString("yyyy-MM-dd HH:mm") + "</pubDate> " + System.Environment.NewLine;
strRSS = strRSS + " <comments>http://www.**.com.cn/ArticleShow@" + ds.Tables[0].Rows[i]["ID"] + ".html</comments> " + System.Environment.NewLine;
strRSS = strRSS + " <slash:comments>0</slash:comments> " + System.Environment.NewLine;
strRSS = strRSS + " <source url='http://www.**.com.cn/ArticleShow@" + ds.Tables[0].Rows[i]["ID"] + ".html'>" + ds.Tables[0].Rows[i]["bt"] + " </source> " + System.Environment.NewLine;
strRSS = strRSS + " <description>" + ds.Tables[0].Rows[i]["nr"] + "</description>" + System.Environment.NewLine;
strRSS = strRSS + " </item>" + System.Environment.NewLine;
}
strRSS = strRSS + " </channel>" + System.Environment.NewLine;
strRSS = strRSS + " </rss>" + System.Environment.NewLine;
return strRSS;
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write(ex.Message);
throw;
}
}
/**//// <summary>
/// 获取RSS数据源
/// 参数:表名。
/// </summary>
/// <remarks>
/// 例如:
/// DataSet ds = GetRSSData(TableName)
/// 注意:根据表的结构,需要调整生成的RSS源。本实例仅供测试用。
/// </remarks>
/// <param name="Tablename">表名</param>
/// <returns>返回一个DataSet 数据源</returns>
public static DataSet GetRSSData(string Tablename)
{
String DBConnStr = System.Configuration.ConfigurationManager.AppSettings["conn"];
System.Data.SqlClient.SqlDataAdapter DataAdapter = new System.Data.SqlClient.SqlDataAdapter();
System.Data.SqlClient.SqlConnection Connection = new System.Data.SqlClient.SqlConnection(DBConnStr);
if (Connection.State != ConnectionState.Open)
{
Connection.Open();
}
try
{
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand("SELECT * from bb ", Connection);
Command.CommandType = CommandType.Text;
//Command.Parameters.Add("@TABLENAME", SqlDbType.VarChar, 20);
//Command.Parameters["@TABLENAME"].Value = Tablename;
//Command.Parameters.Add("@COLUMNS", SqlDbType.VarChar, 2000);
//Command.Parameters["@COLUMNS"].Value = "";
Command.ExecuteNonQuery();
DataAdapter.SelectCommand = Command;
DataSet DataSet = new DataSet();
if (DataSet != null)
{
DataAdapter.Fill(DataSet, "table");
}
return DataSet;
}
catch (System.Exception ex)
{
System.Web.HttpContext.Current.Response.Write(ex.Message);
Connection.Close();
throw;
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ReadRss rss = new ReadRss();
Label1.Text = rss.ProcessRSSItem(TextBox1.Text);
}
}
读取xml类
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>
/// ReadRss 的摘要说明
/// </summary>
public class ReadRss
{
public ReadRss()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string ProcessRSSItem(string rssURL)
{
string str = "";
System.Net.WebRequest myRequest = System.Net.WebRequest.Create(rssURL);
System.Net.WebResponse myResponse = myRequest.GetResponse();
System.IO.Stream rssStream = myResponse.GetResponseStream();
System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();
rssDoc.Load(rssStream);
System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("/rss/channel/item");
string title = "";
string link = "";
string description = "";
for (int i = 0; i < rssItems.Count; i++)
{
System.Xml.XmlNode rssDetail;
rssDetail = rssItems.Item(i).SelectSingleNode("title");
if (rssDetail != null)
{
title = rssDetail.InnerText;
}
else
{
title = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("link");
if (rssDetail != null)
{
link = rssDetail.InnerText;
}
else
{
link = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("link");
if (rssDetail != null)
{
link = rssDetail.InnerText;
}
else
{
link = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("description");
if (rssDetail != null)
{
description = rssDetail.InnerText;
}
else
{
description = "";
}
if (title.Length > 200)
{
title = title.Substring(0, 200);
}
str += "<li><a href='"+link+"' title='"+title+"' target='_blank'>"+title+"</a></li>";
}
return str;
}
public string ReadNews(string rssURL)
{
string str = "";
System.Net.WebRequest myRequest = System.Net.WebRequest.Create(rssURL);
System.Net.WebResponse myResponse = myRequest.GetResponse();
System.IO.Stream rssStream = myResponse.GetResponseStream();
System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();
rssDoc.Load(rssStream);
System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("/rss/channel/item");
string title = "";
string link = "";
string description = "";
string source = "";
for (int i = 0; i < rssItems.Count; i++)
{
System.Xml.XmlNode rssDetail;
rssDetail = rssItems.Item(i).SelectSingleNode("title");
if (rssDetail != null)
{
title = rssDetail.InnerText;
}
else
{
title = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("link");
if (rssDetail != null)
{
link = rssDetail.InnerText;
}
else
{
link = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("link");
if (rssDetail != null)
{
link = rssDetail.InnerText;
}
else
{
link = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("description");
if (rssDetail != null)
{
description = rssDetail.InnerText;
}
else
{
description = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("source");
if(rssDetail!=null)
{
source = rssDetail.InnerText;
}
else
{
source = "";
}
if (title.Length > 200)
{
title = title.Substring(0,200);
}
str += "★<a href='" + link + "' title='" + title + "' target='_blank'>" + title + "</a> 来源:" + source + "<br/>";
}
return str;
}
}