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

相关文章:

  • 2021-11-19
  • 2021-11-17
  • 2022-02-20
  • 2021-10-21
  • 2021-11-15
  • 2021-11-16
猜你喜欢
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-15
相关资源
相似解决方案