private void LoadUrlsFromFile(string filePath) { string filename = Path.GetFileName(filePath).Replace(Path.GetExtension(filePath), "").Trim(); string[] splitData = filename.Split(new char[] { FilenameSeparators.BlockSeparator }); string category = splitData[1].Trim(); int number; int.TryParse(splitData[2], out number); SitemapFile sitemapFile = new SitemapFile() { Filename = filename, Number = number, Category = category }; XmlDocument document = new XmlDocument(); document.Load(filePath); foreach (XmlNode x in document.SelectNodes("urlset/url")) { var entry = new UrlEntry { URL = x.SelectSingleNode("loc").InnerText, ChangeFrequency = Enum.Parse <ChangeFrequency>(x.SelectSingleNode("changefreq").InnerText, true), Category = category, Written = true }; sitemapFile.Entires.Add(entry); // AddUrlEntry(entry); } sitemapsFiles.Add(sitemapFile); }
public void AddUrlEntry(string RelativeUrl, string category, ChangeFrequency changeFrequency, DateTime lastModification) { UrlEntry urlEntry = new UrlEntry { Category = category, ChangeFrequency = changeFrequency, LastModification = lastModification, URL = new Uri(new Uri(this.baseUrl), RelativeUrl).ToString() }; // check if the url is already exists in any of the sitemaps foreach (var sitemapfile in sitemapsFiles) { if (sitemapfile.Entires.Any(w => w.URL == urlEntry.URL)) { return; } } // first lets check if any sitemaps has the same category var sitemapWithSameCategory = sitemapsFiles.Where(w => w.Category.ToLower().Trim() == urlEntry.Category.ToLower().Trim()); if (sitemapWithSameCategory.Count() == 0) { // create new sitemap SitemapFile sitemapFile = new SitemapFile() { Category = urlEntry.Category, Number = 1, Filename = GenerateFilename(urlEntry.Category, 1), NeedReWrite = true }; sitemapFile.Entires.Add(urlEntry); sitemapsFiles.Add(sitemapFile); } else { // Get the latest sitemap within the same category and has less than the maximum allowed entries int num = sitemapWithSameCategory.Max(w => w.Number); var latestSitemap = sitemapWithSameCategory.FirstOrDefault(w => w.Entires.Count < maximumAllowedEntries); if (latestSitemap == null) { // all sitemaps are full, so we need a new sitemap file SitemapFile sitemapFile = new SitemapFile() { Category = urlEntry.Category, Number = num + 1, Filename = GenerateFilename(urlEntry.Category, num + 1), NeedReWrite = true }; sitemapFile.Entires.Add(urlEntry); sitemapsFiles.Add(sitemapFile); } else { // append urlEntry to this sitemaps file latestSitemap.NeedReWrite = true; latestSitemap.Entires.Add(urlEntry); } } }