不少网站都有rss订阅,本人没事也做了一下,你还别说,还有那个意思,本人自己的代码共享一下,希望对大家有所帮助
动态写入rss文件的函数
动态写入rss文件的函数
using System;
using System.IO;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string filename =string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Genetatedrs(string filenames,string content)
{
try
{
FileStream fs = new FileStream(filenames, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.Write(content);
sw.Close();
fs.Close();
}
catch(Exception ee)
{
Response.Write("<script>alert('Generate successfully!');</script>");
}
}
protected string GetXML(int categoryid)
{
string XMLstring="";
string sitehost = "http://www.hinet.com?";
try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=.;database=news;user id=*;password=*;";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from Article_info where CategoryID=" + categoryid;
cmd.Connection = conn;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
XMLstring +=
string.Format(
"<?xml version='1.0' encoding='UTF-8' standalone='yes'?><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/'><channel><title>{0}</title><link>{1}</link><description><![CDATA[{2}]]></description><generator>Text</generator><language>zh-CN</language><copyright>hinet</copyright><pubDate>{3}</pubDate>",
dr["Title"], sitehost, dr["Content"], DateTime.Now);
}
while (dr.Read())
{
XMLstring = XMLstring + "<item><title>" + dr["Title"] + "</title><author>" + dr["Author"] +
"</author><link>" + sitehost + "information.asp?i=" +
dr["InfoID"] + "</link><pubDate>" + dr["PublishDate"] + "</pubDate><guid>" + sitehost +
"information.asp?i=" + dr["InfoID"] + "</guid><description><![CDATA[" + dr["Content"] +
"]]></description><category>" + dr["CategoryID"] + "</category></item>";
}
XMLstring += "</channel></rss>";
}
catch(Exception dd)
{
Response.Write("<script>alert("+dd.Message+");</script>");
}
return XMLstring;
}
protected void Button1_Click(object sender, EventArgs e)
{
int categoryid = Convert.ToInt32(DropDownList1.SelectedValue);
filename = @"F:\asp.net_generate_Rss\XMLnews_"+categoryid+".xml";
string drs = GetXML(categoryid);
Genetatedrs(filename, drs);
}
}