1.在线生成RSS聚合页。
(1)创建Rss.aspx
<%@ Page language="c#" Codebehind="Rss.aspx.cs" AutoEventWireup="false" Inherits="LiTianPing.Rss" %>
只留下这一行,其余的都删掉。
(2)后台代码;Rss.aspx.cs
private void Page_Load(object sender, System.EventArgs e)
{
Response.ContentType="text/xml";
Response.Write(GetRSS());
}
/// <summary>
/// 取得聚合文章
/// </summary>
/// <returns></returns>
public string GetRSS()
{
News t=new News();//自己的业务类
DataSet ds=t.GetListByClass(1);//根据类别得到数据
StringBuilder strCode=new StringBuilder();
strCode.Append("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>");
strCode.Append("<rss version='2.0' xmlns:dc=\"http://purl.org/dc/elements/1.1/\"");
strCode.Append(" xmlns:trackback=\"http://madskills.com/public/xml/rss/module/trackback/\" ");
strCode.Append(" xmlns:wfw=\"http://wellformedweb.org/CommentAPI/\" xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\">");
strCode.Append("<channel>");
strCode.Append("<title>李天平RSSDemo</title>");
strCode.Append("<link>http://"+Request.ServerVariables["SERVER_NAME"]+"</link> ");
strCode.Append("<description>天道酬勤</description> ");
strCode.Append("<copyright>Copyright 2005</copyright> ");
foreach(DataRow row in ds.Tables[0].Rows)
{
string Id=row["Id"].ToString();
string title=row["title"].ToString();
string description=row["description"].ToString();
string pubdate=row["pubdate"].ToString();
string ClassId=row["ClassId"].ToString();
//string author=row["author"].ToString();
strCode.Append("<item>");
strCode.Append("<title>"+title+"</title>");
strCode.Append("<link>http://"+Request.ServerVariables["SERVER_NAME"]+"/NewsShow.aspx?ID="+Id+"</link>");
strCode.Append("<subject>"+description+"</subject>");
strCode.Append("<description><![CDATA["+description+"]]></description>");
strCode.Append("<PubDate>"+pubdate+"</PubDate>");
strCode.Append("<category>"+ClassId+"</category>");
strCode.Append("</item>");
}
strCode.Append("</channel>");
strCode.Append("</rss>");
return strCode.ToString();
}
(3) XmlTextWriter实现方式2;Rss.aspx.cs
string xmlDoc="rss.xml";
private void Page_Load(object sender, System.EventArgs e)
{
xmlDoc=Server.MapPath(".")+xmlDoc;
GetRSS2();
XmlDocument doc= new XmlDocument();
doc.Load(xmlDoc);
Response.ContentType = "text/xml";
doc.Save(Response.Output);
}
/// <summary>
/// 取得聚合文章
/// </summary>
/// <returns></returns>
public void GetRSS2()
{
News t=new News();
DataSet ds=t.GetListByClass(1);
XmlTextWriter writer = new XmlTextWriter(xmlDoc,Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument(true);
writer.WriteComment("RSS页的实现");
writer.WriteStartElement("rss");
writer.WriteAttributeString("version","2.0");
writer.WriteStartElement("channel");
writer.WriteStartElement("title");
writer.WriteString("李天平RSSDemo");
writer.WriteEndElement();
writer.WriteStartElement("link");
writer.WriteString("http://"+Request.ServerVariables["SERVER_NAME"]);
writer.WriteEndElement();
writer.WriteStartElement("description");
writer.WriteString("天道酬勤");
writer.WriteEndElement();
writer.WriteStartElement("copyright");
writer.WriteString("Copyright 2005");
writer.WriteEndElement();
writer.WriteStartElement("language");
writer.WriteString("zh-cn");
writer.WriteEndElement();
foreach(DataRow row in ds.Tables[0].Rows)
{
string Id=row["Id"].ToString();
string title=row["title"].ToString();
string description=row["description"].ToString();
string pubdate=row["pubdate"].ToString();
string ClassId=row["ClassId"].ToString();
//string author=row["author"].ToString();
writer.WriteStartElement("item");
writer.WriteStartElement("title");
writer.WriteString(title);
writer.WriteEndElement();
writer.WriteStartElement("link");
writer.WriteString("http://"+Request.ServerVariables["SERVER_NAME"]+"/NewsShow.aspx?ID="+Id) ;
writer.WriteEndElement();
writer.WriteStartElement("description");
writer.WriteCData(description);
writer.WriteEndElement();
writer.WriteStartElement("pubDate");
writer.WriteString(pubdate);
writer.WriteEndElement();
writer.WriteStartElement("category");
writer.WriteString(ClassId);
writer.WriteEndElement();
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
writer.Close();
}
(责任编辑:admin)