Here's what we do to generate the sitemap for autoquoter.com. See http://www.autoquoter.com/aq/sitemap.xml
var sitemap = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XProcessingInstruction("xml-stylesheet","type=\"text/xsl\" href=\""+ Url.AbsoluteAction("SitemapXsl", "Default") +"\""), new XElement(ns +"urlset", new XAttribute(XNamespace.Xmlns +"sitemap", ns), new XAttribute(XNamespace.Xmlns +"xhtml", xhtml), nodeList)); Response.AddHeader("X-Robots-Tag","noindex"); return Content(sitemap.Declaration+"\r\n"+sitemap, "text/xml");
We also use an xsl stylesheet to transform the sitemap. This helps with browsers that don't provide a decent formatter automatically. See http://www.autoquoter.com/aq/en/Default/SitemapXsl for the stylesheet that we are using.