📄 makerss.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
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.Text;
using System.Xml;
using System.IO;
public partial class WebPage_MakeRss : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int id = 1;
if (Request["ID"] != null)
{
id = Convert.ToInt32(Request["ID"].ToString());
}
CreateRss(id);
}
/// <summary>
/// 这里先执行
/// </summary>
/// <param name="writer"></param>
/// <returns></returns>
public XmlTextWriter WriteRSSPrologue(XmlTextWriter writer)
{
writer.WriteStartDocument();
writer.WriteComment("RSS文档创建于:" + DateTime.Now.ToString("r"));
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
writer.WriteAttributeString("xmlns:blogChannel", "http://www.xblog.com/Index.aspx");
writer.WriteStartElement("channel");
writer.WriteElementString("title", "XBlog");
writer.WriteElementString("link", "http://www.xblog.com/Index.aspx");
writer.WriteElementString("description", "XBlog文章");
writer.WriteElementString("copyright", "Copyright 2006-2006");
writer.WriteElementString("generator", "XBlog1.0");
return writer;
}
/// <summary>
/// 每个写出都用这个
/// </summary>
/// <param name="writer"></param>
/// <param name="sItemTitle">标题</param>
/// <param name="sItemLink">连接</param>
/// <param name="sItemDescription">描述</param>
/// <returns></returns>
public XmlTextWriter AddRSSItem(XmlTextWriter writer, string sItemTitle, string sItemLink, string sItemDescription)
{
writer.WriteStartElement("item");
writer.WriteElementString("title", sItemTitle);
writer.WriteElementString("link", sItemLink);
writer.WriteElementString("description", sItemDescription);
writer.WriteElementString("pubDate", DateTime.Now.ToString("r"));
writer.WriteEndElement();
return writer;
}
/// <summary>
/// Adds a single item to the XmlTextWriter supplied
/// </summary>
/// <param name="writer">The XmlTextWriter to be written to</param>
/// <param name="sItemTitle">The title of the RSS item</param>
/// <param name="sItemLink">The URL of the RSS item</param>
/// <param name="sItemDescription">The RSS item text itself</param>
/// <param name="bDescAsCDATA">Write description as CDATA</param>
/// <returns>The XmlTextWriter with the item info written to it</returns>
public XmlTextWriter AddRSSItem(XmlTextWriter writer, string sItemTitle, string sItemLink, string sItemDescription, bool bDescAsCDATA)
{
writer.WriteStartElement("item");
writer.WriteElementString("title", sItemTitle);
writer.WriteElementString("link", sItemLink);
if (bDescAsCDATA == true)
{
// Now we can write the description as CDATA to support html content.
// We find this used quite often in aggregators
writer.WriteStartElement("description");
writer.WriteCData(sItemDescription);
writer.WriteEndElement();
}
else
{
writer.WriteElementString("description", sItemDescription);
}
writer.WriteElementString("pubDate", DateTime.Now.ToString("r"));
writer.WriteEndElement();
return writer;
}
/// <summary>
/// 最后用这个
/// </summary>
/// <param name="writer"></param>
/// <returns></returns>
public XmlTextWriter WriteRSSClosing(XmlTextWriter writer)
{
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
return writer;
}
public void CreateRss(int ArticleID)
{
DataSet ds = new DataSet();
DAL.boBusiness bo = new DAL.boBusiness();
DAL.MakeConnection Conn = new DAL.MakeConnection();
DAL.clsDBConnkey ConnKey = new DAL.clsDBConnkey();
ConnKey = bo.loadkey();//得到config数据库连接
string sql = "select ID,Title,Content,CreateTime,Summary,IsNominate,ReadTotal,(select count(*) from MessageBoard where MessageBoard.ArticleID=Article.ID )as MessageNum from Article where Article.IsNominate=1 order by CreateTime desc ";
try
{
ds = (DataSet)Conn.MakeConnectionMethod(sql, ConnKey, DAL.executeMethod.execute_DataSet, DAL.
EnumDBType.Sql, "");
if (ds.Tables[0].Rows.Count > 0)
{
XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, System.Text.Encoding.UTF8);
WriteRSSPrologue(writer);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string title = ds.Tables[0].Rows[i]["Title"].ToString();
string Summary = ds.Tables[0].Rows[i]["Summary"].ToString();
string link ="http://"+ Request.ServerVariables["SERVER_NAME"].ToString() + "/Content.aspx?ID=" + ds.Tables[0].Rows[i]["ID"].ToString();
AddRSSItem(writer, title, link, Summary);
}
WriteRSSClosing(writer);
writer.Flush();
writer.Close();
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "text/xml";
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.End();
}
}
catch
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -