之乎者也,阿弥陀佛

软件设计的原则就是,化繁为简,化难为易,把人的思维集中在简单的领域,然后通过有序的组合实现复杂的逻辑。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
2009-08-14 11:24

GetRss.cs类

using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Configuration;

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;

/// <summary>
///GetRss 的摘要说明
/// </summary>
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);
            finfo.Delete();//先删除旧的

            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 =common.RunQuery("select * from tb_news");//common.RunQuery函数为自定查询函数,请自行定义。
            string strRSS = "";
            strRSS = strRSS + "<?xml version=\"1.0\"?> " + System.Environment.NewLine;
            strRSS = strRSS + "<rss xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance/"" + System.Environment.NewLine;
            strRSS = strRSS + "   xmlns:xsd=\"http://www.w3.org/2001/XMLSchema/"   " + System.Environment.NewLine;
            strRSS = strRSS + "   xmlns:slash=\"http://purl.org/rss/1.0/modules/slash//" version=\"2.0\">" + System.Environment.NewLine;
            strRSS = strRSS + "<channel>" + System.Environment.NewLine;
            strRSS = strRSS + "<title>订阅标题</title> " + System.Environment.NewLine;
            strRSS = strRSS + "<link>http://www.Hellocom.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.Hellocom.cn</generator> " + System.Environment.NewLine;
            strRSS = strRSS + "<copyright>北京Hello公司</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]["cname"] + "</title> " + System.Environment.NewLine;
                strRSS = strRSS + "<link>http://www.Hello.com.cn/newsdetail/" + ds.Tables[0].Rows[i]["cid"] + ".html</link> " + System.Environment.NewLine;
                strRSS = strRSS + "<author /> " + System.Environment.NewLine;
                strRSS = strRSS + "<guid>http://www.Hello.com.cn/newsdetail/" + ds.Tables[0].Rows[i]["cid"] + ".html</guid>   " + System.Environment.NewLine;
                strRSS = strRSS + "<pubDate>" + Convert.ToDateTime(ds.Tables[0].Rows[i]["ctime"].ToString()).ToString("yyyy-MM-dd HH:mm") + "</pubDate> " + System.Environment.NewLine;
                strRSS = strRSS + "<comments>http://www.Hello.com.cn/newsdetail/" + ds.Tables[0].Rows[i]["cid"] + ".html</comments>   " + System.Environment.NewLine;
                strRSS = strRSS + "<slash:comments>0</slash:comments>    " + System.Environment.NewLine;
                strRSS = strRSS + "<source url=\"http://www.Hello.com.cn/newsdetail/" + ds.Tables[0].Rows[i]["cid"] + ".html\">" + ds.Tables[0].Rows[i]["cname"] + "</source>    " + System.Environment.NewLine;
                strRSS = strRSS + "<description>" + ds.Tables[0].Rows[i]["cname"] + "</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;
        }
    }

}

===========================================================================

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>
===========================================================================

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string msg="生成成功!";
        string Tablename = "News"; //Request.QueryString["TableName"];
        string fname = System.Web.HttpContext.Current.Server.MapPath("RSS_Folder") + "\\" + Tablename + ".XML";
        try
        {
            GetRss.WriteRss(fname, Tablename);
        }
        catch (System.Exception e11)
        {
            msg="生成时出错!";
        }

        common.MsgBox(msg);
       
    }
}

===========================================================================

建一个文件夹:RSS_Folder

===========================================================================

   common类中的 RunQuery函数

//运行查询
    public static DataSet RunQuery(String QueryString)
    {

        // 声明连接字符串。本示例使用oledb
        //连接到#ZNaboodatabak_En.mdb数据库

        OleDbConnection DBConnection = common.getConn();
        OleDbDataAdapter DBAdapter;
        DataSet ResultsDataSet = new DataSet();

        try
        {

            //运行查询并建立一个数据集
            DBAdapter = new OleDbDataAdapter(QueryString, DBConnection);
            DBAdapter.Fill(ResultsDataSet);

            //关闭数据库连接
            DBConnection.Close();

        }
        catch (Exception ex)
        {

            //如果数据库连接仍然打开则关闭它
            if (DBConnection.State == ConnectionState.Open)
            {
                DBConnection.Close();
            }
            //MsgBox("无法连接到数据库!");
            //System.Web.HttpContext.Current.Response.Write("<script>history.back(1);</script>");
            //System.Web.HttpContext.Current.Response.Write(QueryString+"<br/>");
            //System.Web.HttpContext.Current.Response.Write(ex);
            //System.Web.HttpContext.Current.Response.End();

        }

        return ResultsDataSet;

    }

http://hi.baidu.com/nirvanan/blog/item/a822e3cce2852d580fb3458b.html
posted on 2009-08-27 01:16  搏击的小船  阅读(400)  评论(0编辑  收藏  举报