zoukankan      html  css  js  c++  java
  • 用.NET生成RSS文件,以便Rss阅读器订阅(转)

    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
  • 相关阅读:
    转载 logback的使用和logback.xml详解 http://www.cnblogs.com/warking/p/5710303.html
    HTTP 416
    maven 下载jar失败: Missing artifact javax.jms:jms:jar:1.1
    maven 下载jar失败: resolution will not be reattempted until the update interval of central has elapsed or updates are forced
    Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0
    转载 Servlet3 的 @WebServlet http://www.cnblogs.com/luxh/archive/2012/06/06/2537458.html
    mysqld服务启动失败, Failed to restart mysqld.service: Unit not found.
    <转载> 22种代码味道(Martin Fowler与Kent Beck) http://blog.csdn.net/lovelion/article/details/9301691
    再谈java clone 以及 浅/深拷贝
    设计模式——再谈工厂模式
  • 原文地址:https://www.cnblogs.com/guanjie20/p/1554762.html
Copyright © 2011-2022 走看看