0

I am at a loss for finding a solution to format the result passed to the View from the Controller after implementing SyndicationFeed to return RSS feed in XML form. There are countless solutions of how to use SyndicationFeed, but almost nothing regarding displaying this in a custom style. How might I accomplish this? Please help!

HomeController.cs

public RssActionResult Index()
    {
        SyndicationFeed mainFeed = new SyndicationFeed();

        foreach (var feed in GetRssFeeds())
        {
            Uri feedUri = new Uri(feed);
            SyndicationFeed syndicationFeed;
            using (XmlReader reader = XmlReader.Create(feedUri.AbsoluteUri))
            {
                syndicationFeed = SyndicationFeed.Load(reader);
            }

            syndicationFeed.Id = feed;
            SyndicationFeed tempFeed = new SyndicationFeed(
                mainFeed.Items.Union(syndicationFeed.Items).OrderByDescending(u => u.PublishDate));

            mainFeed = tempFeed;
        }

        return new RssActionResult() { Feed = mainFeed }; 
    }

public class RssActionResult : ActionResult
    {
        public SyndicationFeed Feed { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {

            context.HttpContext.Response.ContentType = "application/rss+xml";
            Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);

            using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
            {
                rssFormatter.WriteTo(writer);
            }
        }
    }

    private static List<string> GetRssFeeds()
    {
        List<string> feeds = new List<string>();

        feeds.Add("http://finance.yahoo.com/rss/headline?s=msft,goog,aapl");

        return feeds;
    }

How to style and display in the view? Preferably within something like the following

@{
ViewBag.Title = "RSS Feed";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

<section id="rssfeed">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-8">
                <h4>The Feed</h4>
                    @* DISPLAY FEED HERE FORMATTED NICELY *@
            </div>
         </div>
     </div>
</section>
Matthew
  • 3,976
  • 15
  • 66
  • 130
  • Maybe I'm missing something but if you return the feed from your action as application/rss+xml content then it is XML not HTML. That's how feed clients can consume it because it's standard. If you embed the XML in the HTML wouldn't it defeat the purpose? – Volkan Paksoy Sep 12 '15 at 23:34
  • It was my understanding that I could consume feed from various sources and render that within a page, like how you see articles in certain areas of webpages for news websites that have been aggregated from various sources. That is what I am trying to accomplish. – Matthew Sep 12 '15 at 23:43

0 Answers0