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 https://www.google.com/sitemaps/protocol.html and here https://developers.google.com/search/docs/advanced/sitemaps/build-sitemap

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 https://search.google.com/search-console/sitemaps

Hey, why not do Bing too? https://www.bing.com/webmasters/sitemaps

Alternate link for submit http://google.com/webmaster and http://bing.com/webmaster

             

        public static void BennySitemap()
        {
            // make some declarations
            ApplicationDbContext db = new ApplicationDbContext();
            XNamespace Xname = "http://www.sitemaps.org/schemas/sitemap/0.9";
            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
        select
            new XElement("url",
                new XElement("loc", "https://www.bennysutton.com/Downloads/Details/" + 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 https://developers.google.com/search/docs/advanced/sitemaps/build-sitemap#sitemapformat
                                                                              //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
            select
                new XElement("url",
                    new XElement("loc", "https://www.bennysutton.com/" + 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
                    downloads
                );
            // 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
            doc.Save(savePath);
        }