Sitecore-Sitemap splitting into multiple files based on languages code

First, generate the sitemap for the Multi-Language website using an any online tool available, it will generate a sitemap for all the pages into a single file with different language code URLs.

Lets split the sitemap to individual language specific sitemap file for easy SEO access.

XDocument document = XDocument.Load("D:\\sitemap.xml");
            var books = from r in document.Descendants("url")
                        select new
                        {
                            loc = r.Element("loc").Value.ToLower(),
                            lastmod = r.Element("lastmod").Value,
                            priority = r.Element("priority").Value

                        };
//EN language sitemap
            XmlTextWriter enwriter = new XmlTextWriter("D:\\sitemap-en.xml", System.Text.Encoding.UTF8);
            enwriter.WriteStartDocument(true);
            enwriter.Formatting = Formatting.Indented;
            enwriter.Indentation = 2;
            enwriter.WriteStartElement("urlset");
            enwriter.WriteAttributeString("xmlns", null, null, "http://www.sitemaps.org/schemas/sitemap/0.9");
            enwriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
            enwriter.WriteAttributeString("xmlns", "schemaLocation", null, "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");

 //ar-AE language sitemap
            XmlTextWriter arAEwriter = new XmlTextWriter("D:\\sitemap-arAE.xml", System.Text.Encoding.UTF8);
            arAEwriter.WriteStartDocument(true);
            arAEwriter.Formatting = Formatting.Indented;
            arAEwriter.Indentation = 2;
            arAEwriter.WriteStartElement("urlset");
            arAEwriter.WriteAttributeString("xmlns", null, null, "http://www.sitemaps.org/schemas/sitemap/0.9");
            arAEwriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
            arAEwriter.WriteAttributeString("xmlns", "schemaLocation", null, "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");

            foreach (var r in books)
            {
//EN language sitemap
                if (r.loc.ToLower().EndsWith("/en") || r.loc.ToLower().EndsWith("/en/") || r.loc.ToLower().Contains("/en/"))
                {

                    enwriter.WriteStartElement("url");
                    enwriter.WriteStartElement("loc");
                    enwriter.WriteString(r.loc);
                    enwriter.WriteEndElement();
                    enwriter.WriteStartElement("lastmod");
                    enwriter.WriteString(r.lastmod);
                    enwriter.WriteEndElement();
                    enwriter.WriteStartElement("priority");
                    enwriter.WriteString(r.priority);
                    enwriter.WriteEndElement();
                    enwriter.WriteEndElement();
                }
//ar-AE language sitemap
                if (r.loc.ToLower().EndsWith("/ar-ae") || r.loc.ToLower().EndsWith("/ar-ae/") || r.loc.ToLower().Contains("/ar-ae/"))
                {

                    arAEwriter.WriteStartElement("url");
                    arAEwriter.WriteStartElement("loc");
                    arAEwriter.WriteString(r.loc);
                    arAEwriter.WriteEndElement();
                    arAEwriter.WriteStartElement("lastmod");
                    arAEwriter.WriteString(r.lastmod);
                    arAEwriter.WriteEndElement();
                    arAEwriter.WriteStartElement("priority");
                    arAEwriter.WriteString(r.priority);
                    arAEwriter.WriteEndElement();
                    arAEwriter.WriteEndElement();
                }          


            }
            enwriter.WriteEndElement();
            enwriter.WriteEndDocument();
            enwriter.Close();

            arAEwriter.WriteEndElement();
            arAEwriter.WriteEndDocument();
            arAEwriter.Close();


           It will create two sitemap files separately.

That’s it 😊


Comments

Popular posts from this blog

Custom Item Url and resolving the item in Sitecore - Buckets

Exploring Sitecore XM Cloud Forms: A Comprehensive Overview

Sitecore Custom Rule (Action and Condition)