string url = "http://127.0.0.1/";
StringBuilder sb = new StringBuilder();
sb.Append(@"<?xml version=""1.0"" encoding=""utf-8""?>");
sb.Append("<rss version=\"2.0\">");
sb.Append("<channel>");
sb.Append("<generator>Me</generator>");
sb.Append("<title>推荐内容</title>");
sb.AppendFormat("<link>{0}</link>", url);
sb.Append("<description><![CDATA[推荐内容]]></description>");
for (int i = 0; i < dt.Rows.Count; i++)
{
sb.Append("<item>");
sb.AppendFormat("<title><![CDATA[{0}]]></title>", dt.Rows[i]["Title"] );
sb.AppendFormat("<author><![CDATA[{0}]]></author>", dt.Rows[i]["UserID"]);
sb.AppendFormat("<description><![CDATA[{0}]]></description>",dt.Rows[i]["Title"]);
sb.AppendFormat("<pubDate>{0}</pubDate>", Convert.ToDateTime(dt.Rows[i]["CreateDate"].ToString()).ToString("yyyy-MM-dd HH:mm"));
sb.AppendFormat("<link><![CDATA[{0}{1}{2}]]></link> ", ID","UserID");
sb.Append("</item>");
}
sb.Append("</channel>");
sb.Append("</rss>");
return sb.ToString();
页面输出
Rss rss = new Rss();
// 输出 MIME 类型
Response.ContentType = "application/xml";
// 编码
Response.ContentEncoding = Encoding.UTF8;
Response.Write(rss.GetRSS());
Response.Flush();
Response.Close();