Benny Sutton is 4 hire - just click here

How To Create a Google XML Sitemap in One.Net

All your hard work on your website will be in vain if it is not indexed (and therefore is undiscovered).

This is how to create a google compliant sitemap in XML  format 1) from records in a database and 2) from a list

Google gives contradictory advice how to do that here and here

This code creates a file that complies with Google Sitemaps. Add your own Table names, fieldnames etc. and you're good to go!

then submit at

Hey, why not do Bing too?

Alternate link for submit and


        public static void BennySitemap()
            // make some declarations
            ApplicationDbContext db = new ApplicationDbContext();
            XNamespace Xname = "";
            int _companyID = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["CompanyID"].ToString());

            // we have some urls outside of the database too so we'll use Linq to add the XElements from this list
            List<string> names = new List<string> { "Downloads", "Downloads/SiteMap", "Downloads/?sortOrder=Popular", "Downloads/?sortOrder=Latest", "Downloads/?sortOrder=Name", "Downloads/?sortOrder=Oldest" };

            ///# 1) how to fill from a database table
            var downloads =
        new XElement(Xname + "urlset", // note we slipped in the XNamespace Xname there 
        from c in db.Downloads // specify your table name
        .ToList() // leave this - important
        where c.Flag != true && c.CompanyID == WebConfig.CompanyID() // optional arguments
        orderby c.DownloadID descending
            new XElement("url",
                new XElement("loc", "" + c.DownloadID + "-" + c.Slug),  // specify your display fields
                new XElement("lastmod", c.DateCreated.ToString("yyyy-MM-dd")) // this date format is vital - Google will not validate your sitemap! 
                                                                              ////  as of 2021 Google ignores changefreq and priority - but you can uncomment these next lines if you like
                                                                              ////  see
                                                                              //new XElement("changefreq", "monthly"),   
                                                                              //new XElement("priority", 0.8)
            ), // yes, this line has a comma!

            ///# 2) now let's create some elements from the list we created earlier
            from n in names
                new XElement("url",
                    new XElement("loc", "" + n),
                    new XElement("lastmod", DateTime.Now.ToString("yyyy-MM-dd"))

            // create the XDocument in memory
            var doc =
                new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"), // utf-8 encoding - very important
            // where to save the file - important to save it at the website root as this is where Google expects sitemaps to be
            string savePath = Path.Combine(HttpContext.Current.Server.MapPath("~/"), "sitemap.xml");
            // save the doc to an xml file