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);    
    }
}
­

转载于:https://www.cnblogs.com/Sangy/archive/2008/02/20/1075045.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值