Skip to content

Instantly share code, notes, and snippets.

@azcoov
Created March 21, 2011 05:02
Show Gist options
  • Save azcoov/879061 to your computer and use it in GitHub Desktop.
Save azcoov/879061 to your computer and use it in GitHub Desktop.
Render a sitemap in asp.net mvc
using System;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;
namespace MvcSiteMap.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ContentResult Index()
{
//Scraping SO feed
var url = "http://stackoverflow.com/feeds";
XmlReader reader = XmlReader.Create(url);
SyndicationFeed feed = SyndicationFeed.Load(reader);
var q = from item in feed.Items
select new {
Title = item.Title,
URL = item.Id,
Date = item.PublishDate
};
//Build the SiteMap
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
var sitemap = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement(ns + "urlset",
from i in q
select
new XElement(ns + "url",
new XElement(ns + "loc", i.URL),
new XElement(ns + "lastmod", String.Format("{0:yyyy-MM-dd}", i.Date)),
new XElement(ns + "changefreq", "monthly"),
new XElement(ns + "priority", "0.5")
)
)
);
return Content(sitemap.ToString(), "text/xml");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment