Create -RSS-Feed-in-using-LINQ-to-XML

28-Apr-2014 0 Comments paras

Here is a very simple way to create rss feed in ASP.NET using LINQ to XML. Here I have created the rss feed for this blog, the url for the feed is http://www.codesack.com/feed.aspx

 protected void Page_Load(object sender, EventArgs e)
    {
        List lstPost = BlogController.GetPosts(); ;
        
         Response.Clear();
          Response.ContentType = "text/xml";
     
            XDocument document = new XDocument(
                new XDeclaration("1.0", "utf-8", null),
                new XElement("rss",
                   new XElement("channel",
                       new XElement("title", "Codesack Blog Posts"),
                       new XElement("link", "http://www.codesack.com"),
                       new XElement("description", "Blogs from Codesack"),
                               from p in lstPost
                               orderby p.PostTitle
                               select new XElement("item",
                                   new XElement("title", p.PostTitle ),
                                   new XElement("description", p.PostSummary),
                                   new XElement("link", string.Format("http://www.codesack.com/blog/{0}",Regex.Replace(p.PostTitle, @"\s", "-")))
                               )
                       ),
                   new XAttribute("version", "2.0")));
           document.Save(Response.Output);
           Response.End();
       }
    }
Comments