C# / .NETDevOpsMisc
C# / .NET
Create an XML Sitemap
Alexandru Puiu
Alexandru Puiu
September 16, 2012
1 min

Table Of Contents

01
RSS is something quite standard, and as such, is easy to read and create.
02
And finally, we create a Controller Method to create the RSS

RSS is something quite standard, and as such, is easy to read and create.

We start off with a FeedResult class, which inherits from ActionResult

using System;
using System.ServiceModel.Syndication;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace Data.Controllers
{
    public class FeedResult : ActionResult
    {
        public Encoding ContentEncoding { get; set; }
        public string ContentType { get; set; }
        private readonly SyndicationFeedFormatter feed;

        public SyndicationFeedFormatter Feed{
            get { return feed; }
        }

        public FeedResult(SyndicationFeedFormatter feed) {
            this.feed = feed;
        }

        public override void ExecuteResult(ControllerContext context) {
            if (context == null)
                throw new ArgumentNullException("context");
            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = !string.IsNullOrEmpty(ContentType) ?
                ContentType : "application/rss+xml";
            if (ContentEncoding != null)
                response.ContentEncoding = ContentEncoding;
            if (feed != null)
                using (var xmlWriter = new XmlTextWriter(response.Output)) {
                    xmlWriter.Formatting = Formatting.Indented;
                    feed.WriteTo(xmlWriter);
                }
        }
    }
}

And finally, we create a Controller Method to create the RSS

public ActionResult MyRss()
{
    var postItems = new List();
    using (Repository rep = new Repository())
    {
        var list = rep.GetData();
        if (list != null && list.Count() > 0)
        {
            var itemList = list.Take(20);
            foreach (var item in itemList)
            {
                postItems.Add(
                    new SyndicationItem(
                        item.Title, //a Title for the RSS Item
                        item.Description, //a short description for the Item
                        new Uri("http://example.com/" + item.Link) //a link to this item
                    )
                );
            }
        }
    }
    var feed = new SyndicationFeed("My Site Name", "", new Uri("http://example.com/"), postItems)
    {
        Copyright = new TextSyndicationContent("Copyright " + DateTime.Now.Year + " My Site",
                        TextSyndicationContentKind.Plaintext),
        Language = "en-US"
    };
    return new FeedResult(new Rss20FeedFormatter(feed));
}

Tags

utils
Alexandru Puiu

Alexandru Puiu

Engineer / Security Architect

Systems Engineering advocate, Software Engineer, Security Architect / Researcher, SQL/NoSQL DBA, and Certified Scrum Master with a passion for Distributed Systems, AI and IoT..

Expertise

.NET
RavenDB
Kubernetes

Social Media

githubtwitterwebsite

Related Posts

RavenDB Integration Testing
Using RavenDB in Integration Testing
December 24, 2022
2 min

Subscribe To My Newsletter

I'll only send worthwhile content I think you'll want, less than once a month, and promise to never spam or sell your information!
© 2023, All Rights Reserved.

Quick Links

Get In TouchAbout Me

Social Media