当前位置:首页 > ASP.NET生成RSS2.0标准文件

ASP.NET生成RSS2.0标准文件

点击次数:1060  更新日期:2010-12-30
\n

RSS正在改变着人们的阅读习惯,一时间大部份网站都支持了RSS输出。自己写了个ASP.NET生成RSS2.0标准文件的方法。我用的是文件流输入XML文件,话不多说,请看下面代码:


\n

StringBuilder sb = new StringBuilder();


\n

sb.Append(“”);


\n

sb.Append(System.Environment.NewLine);


\n

sb.Append(“”);


\n

sb.Append(System.Environment.NewLine);


\n

sb.Append(” “);


\n

sb.Append(System.Environment.NewLine);


\n

sb.Append(” “);


\n

sb.Append(System.Environment.NewLine);


\n

sb.Append(” http://www.web2bar.cn “);


\n

sb.Append(System.Environment.NewLine);


\n

sb.Append(” ” + title + “”);


\n

sb.Append(System.Environment.NewLine);


\n

WebBar.BLL.BArticle bArticle = new WebBar.BLL.BArticle();


\n

IList<ArticleEntity> ArticleEntitys = bArticle.List(int.Parse(ddlRssCount.SelectedValue.ToString()), int.Parse(ddlChannelID.SelectedValue.ToString()));


\n

foreach (ArticleEntity ae in ArticleEntitys)


\n

{


\n

sb.Append(” “);


\n

sb.Append(System.Environment.NewLine);


\n

sb.Append(” “);


\n

sb.Append(System.Environment.NewLine);


\n

sb.Append(” http://www.web2bar.cn/Article/” + ae.ArticleID.ToString() + “.aspx “);


\n

sb.Append(System.Environment.NewLine);


\n

sb.Append(” +ae.Description+”]]>”);


\n

sb.Append(System.Environment.NewLine);


\n

sb.Append(” “+ae.ArticleAuthor+”");


\n

sb.Append(System.Environment.NewLine);


\n

sb.Append(” “+ae.ArticleCreateTime.ToString(“yyyy-MM-dd HH:mm”)+” “);


\n

sb.Append(System.Environment.NewLine);


\n

sb.Append(” ” + ae.CommentCount.ToString() + “”);


\n

sb.Append(System.Environment.NewLine);


\n

sb.Append(” “);


\n

sb.Append(System.Environment.NewLine);


\n

}


\n

sb.Append(” “);


\n

sb.Append(System.Environment.NewLine);


\n

sb.Append(“”);


\n

try


\n

{


\n

using (FileStream fs = new FileStream(Server.MapPath(xmlFileName), FileMode.Create, FileAccess.Write, FileShare.Write))


\n

{


\n

using (StreamWriter streamwriter = new StreamWriter(fs, Response.ContentEncoding))


\n

{


\n

streamwriter.Write(sb);


\n

Common.JsUtility.Alert(“成功生成RSS聚合内容”);


\n

}


\n

}


\n

}


\n

生成的XML文件格式如下:http://www.web2bar.cn/XML/WebBarRss.xml


\n

来源:http://www.cnblogs.com/iawsky

\n