小光网络技术博客

介绍和研究搜索引擎优化、SEO、asp、asp.net、php、jsp技术

« PHP的语言层面的优化以及代码优化技巧Asp.net 配置--关于无法找到该页的解决方法 »

ASPX生成RSS

rss.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="rss.aspx.cs" Inherits="Solorez.RSS" Debug="true" %>

rss.aspx.cs:

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Solorez
{
    partial class RSS : System.Web.UI.Page
    {
        string strRSS = "";
        private void Page_Load(object sender, System.EventArgs e)
        {
            Response.ContentType = "application/xml"; // 输出并按xml数据显示
            Response.Write(GetRSS());
        }

        public string GetRSS()
        {
            DataTable dt = GetNews(); // 调用GetNews()方法,获得数据
            string br = Environment.NewLine;
            strRSS = strRSS + "<rss version=\"2.0\">" + br;
            strRSS = strRSS + "<channel>" + br;
            strRSS = strRSS + "<title>Solorez新闻</title>" + br;
            strRSS = strRSS + "<link>http://hi.baidu.com/solorez</link>" + br;
            strRSS = strRSS + "<description>欢迎订阅Solorez</description>" + br;
            for (int i = 0; i < dt.Rows.Count; i++);{
            strRSS = strRSS + "<item>" + br;
            strRSS = strRSS + "<title><![CDATA[" + dt.Rows[i]["Heading"] + "]]></title>" + br;
            strRSS = strRSS + "<link>" + "http://hi.baidu.com/solorez/news.aspx?id=" + dt.Rows[i]["id"] + "</link> " + br;
            strRSS = strRSS + "<description><![CDATA[" + dt.Rows[i]["NewsContent"] + "]]></description>" + br;
            strRSS = strRSS + "<copyright>Solorez</copyright>" + br;
            strRSS = strRSS + "<pubDate>" + dt.Rows[i]["InsertTime"] + "</pubDate>" + br;
            strRSS = strRSS + "</item>" + br;
            strRSS = strRSS + "</channel>" + br;
            strRSS = strRSS + "</rss>";
            }
            return strRSS;
        }

        public DataTable GetNews()
        {
            SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
            DataTable newsTable = new DataTable();
            string queryString = "SELECT TOP 10 ID,InsertTime,Heading,NewsContent FROM News ORDER BY InsertTime DESC";
            SqlDataAdapter adapter = new SqlDataAdapter(queryString,connection);
            adapter.Fill(newsTable);
            return newsTable;       
        }
    }
}
  • quote 1.果棟
  • 哥们
    在你博客里面加一个站内搜索引擎会更好啦
  • 2008-9-6 3:01:30 回复该留言

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Sam

Copyright 2007-2008 小光. Some Rights Reserved.