locked
Dynamic Sitemap generation RRS feed

  • Question

  • User-910361580 posted

    Hi 

    I am using webpages and have created a file sitemap.vbhtml to generate my sitemap dynamically. But I want the latest sitemap to be available to anyone who searches for domain.com/sitemap.xml. Currently domain.com/sitemap gets the latest xml and sitemap.xml returns a 404, ofcourse. Please guide.

    Thanks

    Thursday, December 17, 2015 6:52 PM

Answers

  • User-1327394822 posted
    @using System.Xml.Linq;
    @{
        Layout = null;
        var urls = new List<string>{
                    "default","about", "contact"
        };
        XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
        var baseurl = "http://yourdomain.com/{0}";
        var sitemap = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
                new XElement(ns + "urlset",
                    from url in urls select
                    new XElement("url",
                        new XElement("loc", string.Format(baseurl, url)),
                        new XElement("lastmod", string.Format("{0:yyyy-MM-dd}", DateTime.Now)),
                        new XElement("changefreq", "monthly"),
                        new XElement("priority", "0.5")
                    )
                )  
        );
        Response.ContentType = "text/xml";
        sitemap.Save(Response.Output);
    }

    Note: I have used it in cshtml. For detailed explaination refer the book, webmatrix by Mike Brind..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 18, 2015 6:49 AM

All replies

  • User325035487 posted

    https://sohelelite.wordpress.com/2013/03/04/create-google-sitemap-xml-using-c/

    Thursday, December 17, 2015 7:26 PM
  • User-1327394822 posted
    @using System.Xml.Linq;
    @{
        Layout = null;
        var urls = new List<string>{
                    "default","about", "contact"
        };
        XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
        var baseurl = "http://yourdomain.com/{0}";
        var sitemap = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
                new XElement(ns + "urlset",
                    from url in urls select
                    new XElement("url",
                        new XElement("loc", string.Format(baseurl, url)),
                        new XElement("lastmod", string.Format("{0:yyyy-MM-dd}", DateTime.Now)),
                        new XElement("changefreq", "monthly"),
                        new XElement("priority", "0.5")
                    )
                )  
        );
        Response.ContentType = "text/xml";
        sitemap.Save(Response.Output);
    }

    Note: I have used it in cshtml. For detailed explaination refer the book, webmatrix by Mike Brind..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 18, 2015 6:49 AM