我知道的两种,以前就想写了。有经验的都知道,只是为了给没有接触过的人提供点东西,可以参考。
第一种方式:
直接在前台页面创建格式xml
<?xml version="1.0" ?>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RSSAspx.aspx.cs" Inherits="WebApplication1.RSSAspx" %>
<rss version="2.0">
<channel>
<title>网站标题</title>
<description>也许很牛逼但是很牛逼还是很牛逼</description>
<link>网站地址</link>
<language>zh-cn</language>
<asp:Repeater runat="server" id="re">
<ItemTemplate>
<item>
<title><%#Eval("PTitle") %></title>
<link><%#Eval("PId", "http://localhost:9988/BookDetail.aspx?id={0}")%></link>
<pubDate><%#Eval("PTime") %></pubDate>
<source>XXX</source>
<author><![CDATA[<%#Eval("PUrl") %>]]></author>
<description><![CDATA[<%#Eval("PDes")%>]]></description> <%--<![CDATA[]]>是显示文字如果有换行则给他添加<br />并且加结束标志--%>
</item>
</ItemTemplate>
</asp:Repeater>
</channel>
</rss>
后台代码,从数据获取数据绑定就可以了。
News news = new News();
DataSet ds = news.GetListByClass(5);
re.DataSource = ds;
re.DataBind();
第二种方式:直接后台拼格式xml
前台只需要保留第一行
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Rss.aspx.cs" Inherits="WebApplication1.Rss" %>
后台:
namespace WebApplication1
{
public partial class Rss : System.Web.UI.Page
{
string xmlDoc = "rss.cml";
protected void Page_Load(object sender, EventArgs e)
{
ITemplate te= re.HeaderTemplate;
xmlDoc = Server.MapPath(".") + xmlDoc;
GetRss();
XmlDocument doc = new XmlDocument();
doc.Load(xmlDoc);
Response.ContentType = "text/xml";
doc.Save(Response.Output);
}
public void GetRss()
{
News news = new News();
DataSet ds = news.GetListByClass(5);//获取数据
XmlTextWriter writer = new XmlTextWriter(xmlDoc, Encoding.UTF8);
writer.Formatting = Formatting.Indented;//设这缩进
writer.WriteStartDocument(true);//写入xml声明
writer.WriteComment("RSS的实现");
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
writer.WriteStartElement("channel");
writer.WriteStartElement("title");
writer.WriteString("jiyong的Rss");
writer.WriteEndElement();
writer.WriteStartElement("link");
writer.WriteString("http://" + Request.ServerVariables["SERVER_NAME"]);
writer.WriteEndElement();
writer.WriteStartElement("description");
writer.WriteString("xxxx");
writer.WriteEndElement();
writer.WriteStartElement("copyringht");
writer.WriteString("2005");
writer.WriteEndElement();
writer.WriteStartElement("language");
writer.WriteString("zh-CN");
writer.WriteEndElement();
foreach (DataRow row in ds.Tables[0].Rows)//遍历表中数据
{
//PId, PTypeId, PUserId, PTitle, PUrl, PDes, PClicks, PTime, PUp, PDown
string NewId = row["PId"].ToString();
string Heading = row["PTitle"].ToString();
string content = row["PDes"].ToString();
string IssuDate = row["PTime"].ToString();
string ClassId = row["PTypeId"].ToString();
writer.WriteStartElement("item");
writer.WriteStartElement("title");
writer.WriteString(Heading);
writer.WriteEndElement();//结束
writer.WriteStartElement("link");
writer.WriteString("http://" + Request.ServerVariables["SERVER_NAME"] + "/NewsShow?ID=" + NewId);
writer.WriteEndElement();//
writer.WriteStartElement("description");
writer.WriteString(content);
writer.WriteEndElement();//
writer.WriteStartElement("PubDate");
writer.WriteString(IssuDate);
writer.WriteEndElement();
writer.WriteStartElement("category");
writer.WriteString(ClassId);
writer.WriteEndElement();
writer.WriteEndElement();
}
writer.WriteEndElement(); writer.WriteEndElement();
writer.Flush();
writer.Close();
}
}
}
转载于:https://blog.51cto.com/hacjj/1303231