Create, show and save XML Sitemap Dynamically - .Net Core

Feb 24, 2019 BY xtreme

WHAT IS XML SITEMAP

An XML (EXTENSIBLE MARKUP LANGUAGE) SITEMAP Is a Text File USED TO DETAIL ALL URLS ON A WEBSITE. IT CAN INCLUDE EXTRA INFORMATION (METADATA) ON EACH URL, WITH DETAILS OF WHEN THEY WERE LAST UPDATED, HOW IMPORTANT THEY ARE AND WHETHER THERE ARE ANY OTHER VERSIONS OF THE URL CREATED IN OTHER LANGUAGES. ALL OF THIS IS DONE TO HELP THE SEARCH ENGINES CRAWL YOUR WEBSITE MORE EFFICIENTLY, ALLOWING ANY CHANGES TO BE FED TO THEM DIRECTLY, INCLUDING WHEN A NEW PAGE IS ADDED OR AN OLD ONE REMOVED.

CONTROLLER

PUBLIC CLASS HOMECONTROLLER : CONTROLLER {
    PUBLIC ACTIONRESULT ARTICLE()
    {
       VAR SITEMAPNODES = _SITEMAP.GETSITEMAPNODES(THIS.URL, "ARTICLES");
       STRING XML = _SITEMAP.GETSITEMAPDOCUMENT(SITEMAPNODES);
       SAVEXMLATROOT("ARTICLES.XML", XML);
       RETURN THIS.CONTENT(XML, "APPLICATION/XML", ENCODING.UTF8);
    }
}


SERVICE


USING SYSTEM;
USING SYSTEM.COLLECTIONS.GENERIC;
USING SYSTEM.GLOBALIZATION;
USING SYSTEM.XML.LINQ;
USING SYSTEM.WEB.MVC;
USING BLOGSEO.HELPERS;
USING SYSTEM.THREADING.TASKS; USING SYSTEM.LINQ;
PUBLIC STRING GETSITEMAPDOCUMENT(IENUMERABLE<SITEMAPNODE> SITEMAPNODES)
{
    XNAMESPACE XMLNS = "HTTP://WWW.SITEMAPS.ORG/SCHEMAS/SITEMAP/0.9";
    XELEMENT ROOT = NEW XELEMENT(XMLNS + "URLSET");
    FOREACH (SITEMAPNODE SITEMAPNODE IN SITEMAPNODES) {
        XELEMENT URLELEMENT = NEW XELEMENT(
        XMLNS + "URL",
        NEW XELEMENT(XMLNS + "LOC", URI.ESCAPEURISTRING(SITEMAPNODE.URL)),
            SITEMAPNODE.LASTMODIFIED == NULL ? NULL : NEW XELEMENT(
                XMLNS + "LASTMOD",
                SITEMAPNODE.LASTMODIFIED.VALUE.TOLOCALTIME().TOSTRING("YYYY-MM-DDTHH:MM:SSZZZ")),
                SITEMAPNODE.FREQUENCY == NULL ? NULL : NEW XELEMENT(
                XMLNS + "CHANGEFREQ",
                SITEMAPNODE.FREQUENCY.VALUE.TOSTRING().TOLOWERINVARIANT()),
                SITEMAPNODE.PRIORITY == NULL ? NULL : NEW XELEMENT(
                XMLNS + "PRIORITY",
                SITEMAPNODE.PRIORITY.VALUE.TOSTRING("F1", CULTUREINFO.INVARIANTCULTURE)));
                ROOT.ADD(URLELEMENT);
    }

    XDOCUMENT DOCUMENT = NEW XDOCUMENT(ROOT);
    RETURN DOCUMENT.TOSTRING();
}

PUBLIC IREADONLYCOLLECTION<SITEMAPNODE> GETSITEMAPNODES(URLHELPER URLHELPER, STRING TYPE){
    LIST<SITEMAPNODE> NODES = NEW LIST<SITEMAPNODE>();
    IENUMERABLE<ARTICLEMODEL> ARTICLES = _ARTICLESERVICE.GETALL().WHERE(R => R.SLUG !=NULL);
   foreach (ArticleModel article in articles) {                   
       nodes.Add(
           new SitemapNode() {
               Url = urlHelper.AbsoluteRouteUrl("articleDetailRoute", new { articleId = article.Id, slug = article.Slug }).Replace("seo.",""),
               Frequency = SitemapFrequency.Weekly,
               Priority = 0.8
           });
   }
   return nodes.Where(n => n.Url != null).ToList<SitemapNode>();       
}


MODEL

public class ArticleModel{

    public ArticleModel(); public string Id { get; set; } [Display(Name = "Slug")] public string Slug { get; set; } }

Related