RSS文件输出(ASP.NET版)

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

StringBuilder sb = new StringBuilder();

            sb.Append("");

            sb.Append(System.Environment.NewLine);

            sb.Append("");

            sb.Append(System.Environment.NewLine);

            sb.Append(");

            sb.Append(System.Environment.NewLine);

            sb.Append("    ");

            sb.Append(System.Environment.NewLine);

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

            sb.Append(System.Environment.NewLine);

            sb.Append("    " + title + "");

            sb.Append(System.Environment.NewLine);

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

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

            foreach (ArticleEntity ae in ArticleEntitys)

            {

                sb.Append("    ");

                sb.Append(System.Environment.NewLine);

                sb.Append("      ");

                sb.Append(System.Environment.NewLine);

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

                sb.Append(System.Environment.NewLine);

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

                sb.Append(System.Environment.NewLine);

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

                sb.Append(System.Environment.NewLine);

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

                sb.Append(System.Environment.NewLine);

                sb.Append("      " + ae.CommentCount.ToString() + "");

                sb.Append(System.Environment.NewLine);

                sb.Append("    ");

                sb.Append(System.Environment.NewLine);

            }

            sb.Append(" ");

            sb.Append(System.Environment.NewLine);

            sb.Append("");

            try

            {

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

                {

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

                    {

                        streamwriter.Write(sb);

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

                    }

                }

            }

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

posted on 2008-04-08 09:50  爱问天  阅读(427)  评论(0编辑  收藏  举报