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