Ejemplo n.º 1
0
        /// <summary>
        /// Create Sitemap Index file - this should include all the gzip's
        /// </summary>
        public void CreateSitemapIndex()
        {
            using (Stream fs = OpenSitemapIndexFile())
            {
                using (XmlTextWriter writer = new XmlTextWriter(fs, Encoding.UTF8))
                {
                    writer.WriteStartDocument();
                    writer.WriteStartElement("urlset");
                    writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");

                    DirectoryInfo directorySelected = new DirectoryInfo(SitemapsFolderPath);
                    foreach (FileInfo file in directorySelected.GetFiles())
                    {
                        if ((File.GetAttributes(file.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden &
                            file.Extension == ".gz")
                        {
                            writer.WriteStartElement("url");
                            writer.WriteElementString("loc", RootUrl.TrimWith('/') + "sitemaps/" + file.Name);
                            writer.WriteElementString("lastmod", file.LastWriteTime.ToString());
                            writer.WriteEndElement();
                        }
                    }

                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.Flush();
                }
                fs.Close();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a sitemap index file for each file specified
        /// </summary>
        public void CreateSitemapIndex(IEnumerable <string> files)
        {
            //compress all files in sitemap folder to gz
            if (GZipSitemap)
            {
                CompressFiles();
            }

            // create main sitemap file
            using (Stream fs = OpenSitemapIndexFile())
            {
                using (XmlTextWriter writer = new XmlTextWriter(fs, Encoding.UTF8))
                {
                    writer.WriteStartDocument();
                    writer.WriteStartElement("sitemapindex");
                    writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");

                    // create a link to each compressed sitemap file
                    foreach (string file in files)
                    {
                        string loc = RootUrl.TrimWith('/') + SitemapsFolderName.TrimWith('/') + file + SitemapWriter.SitemapFileExtension;
                        if (GZipSitemap)
                        {
                            loc += ".gz";
                        }

                        writer.WriteStartElement("sitemap");
                        writer.WriteElementString("loc", loc);
                        writer.WriteElementString("lastmod", DateTime.Now.ToString());
                        writer.WriteEndElement();
                    }

                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.Flush();
                }
                fs.Close();
            }
        }