一、介绍
RSS也叫聚合RSS,是在线共享内容的一种简易方式
Book业务类
在WebForm1.aspx中
在WebForm2.aspx.cs中
RSS也叫聚合RSS,是在线共享内容的一种简易方式
通常在时效性比较强的内部上使用RSS订阅能更快速获取信息,网站提供RSS输出,有利于让用户获取网站内容的最新更新等。
二、简单配置(不搭三层)
1、在Test数据库中 创建一个用于书的表格,并存放一些图书记录
create table BookList
(
BookID int identity(1,1) primary key,
BookName nvarchar(100) not null,
BookDate datetime,
BookAuthor nvarchar(100),
BookISBN nvarchar(50),
BookDescription nvarchar(500)
)
随便插两条记录
2、在项目中添加
Book实体类
public class Book
{
public int BookID { get; set; }
public string BookName { get; set; }
public DateTime BookDate { get; set; }
public string BookAuthor { get; set; }
public string BookISBN { get; set; }
public string BookDescription { get; set; }
}
Book业务类
public class BookManager
{
//拿到图书集合
public List<Book> GetBookList()
{
DataTable dt = new DataTable();
string constr = "server=.;database=studentdb;uid=sa;pwd=123456";
string cmdText = "select * from booklist";
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
}
}
}
return DTToList(dt);
}
//将DataTable对象转换为Book集合
public List<Book> DTToList(DataTable dt)
{
List<Book> list = new List<Book>();
foreach (DataRow dr in dt.Rows)
{
Book book = new Book();
book.BookAuthor = dr["BookAuthor"].ToString();
DateTime bookdate = new DateTime();
DateTime.TryParse(dr["BookDate"].ToString(),out bookdate);
book.BookDate = bookdate;
book.BookDescription = dr["BookDescription"].ToString();
int bookid = 0;
int.TryParse(dr["BookID"].ToString(),out bookid);
book.BookID = bookid;
book.BookISBN = dr["BookISBN"].ToString();
book.BookName = dr["BookName"].ToString();
list.Add(book);
}
return list;
}
}
在WebForm1.aspx中
<?xml version="1.0" ?>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="RSSApp.WebForm1" %>
<rss version="2.0">
<channel>
<title>XXX书店</title>
<description>这是一个很好的书店</description>
<language>zh-cn</language>
<%=strbooks %>
</channel>
</rss>
在WebForm2.aspx.cs中
protected string strbooks;
protected void Page_Load(object sender, EventArgs e)
{
BookManager bll = new BookManager();
List<Book> list= bll.GetBookList();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Count; i++)
{
sb.Append("<item>");
sb.Append(string.Format("<title>{0}</title>",list[i].BookName));
sb.Append(string.Format("<pubDate>{0}</pubDate>",list[i].BookDate));
sb.Append("<source>XX书店</source>");
sb.Append(string.Format("<author>{0}</author>",list[i].BookAuthor));
sb.Append(string.Format("<description>{0}</description>",list[i].BookDescription));
sb.Append("</item>");
}
strbooks = sb.ToString();
}