Sunday, 13 October 2013

Dynamically Sitemap Generator in ASP.NET using C# with limited links

 protected void GenrateBtn_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            Sitemap sitemap = new Sitemap();
            string FilePath = @"C:\Documents and Settings\LandKhoj.com\Desktop\Stiemap.xls";
            dt = ImportFromExcel(FilePath);

            sitemap.Create(dt, "", "", "", Server.MapPath("~/sitemap"));
        }




using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace BusinessLogicLayer
{
    public class Sitemap
    {
        public void Create(DataTable dtUrl, string LastModified, string changefreq, string priorty, string path)
        {
            try
            {
                //DeleteFile(path);
                int Loop = 0, start = 0, end = 0, rem = 0, count=0;
                string sb=string.Empty;
                double TotalUrl = dtUrl.Rows.Count;
                for (int i = 0; i < TotalUrl; i++)
                    if (i % 5 == 0)
                        Loop++;
                int vv = (int)TotalUrl % 5;
                if (TotalUrl % 5 != 0 && TotalUrl>5)
                {
                    Loop--;
                    rem = (int)TotalUrl % 5;
                    end=4;
                }
                else if (TotalUrl % 5 == 0 && TotalUrl > 5)
                {
                    rem = 0;
                    end = 5;
                }
                else if (TotalUrl % 5 != 0 && TotalUrl < 5)
                {
                    rem = (int)TotalUrl;
                    end = rem;
                }
                for (int i = 0; i < Loop; i++)
                {
                    sb = string.Empty;
                    sb = Value(dtUrl, start, end, LastModified, changefreq, priorty);
                    start = start + 5;
                    end = end + 5;
                    count++;
                    if (i == 0)
                        File.AppendAllText(path + @"\sitemap.xml", sb.ToString());
                    else
                        File.AppendAllText(path + @"\sitemap" + (count - 1).ToString() + ".xml", sb.ToString());
                }
                if (rem != 0 && TotalUrl > 5)
                {
                    sb = string.Empty;
                    sb = Value(dtUrl, start, start + rem, LastModified, changefreq, priorty);
                    File.AppendAllText(path + @"\sitemap" + count.ToString() + ".xml", sb.ToString());
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string Value(DataTable dt, int Start, int End, string LastModified, string changefreq, string priorty)
        {
            double tot = dt.Rows.Count;
            StringBuilder sb = new StringBuilder();
            //<?xml version="1.0" encoding="UTF-8"?>
            //<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

            sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.Append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">");
            for (int i = Start; i < End; i++)
            {
                sb.Append("<url>");
                sb.Append("<loc>");
                sb.Append(dt.Rows[i][0].ToString());
                sb.Append("</loc>");
                sb.Append("<lastmod>");
                sb.Append(LastModified);
                sb.Append("</lastmod>");
                sb.Append("<changefreq>");
                sb.Append(changefreq);
                sb.Append("</changefreq>");
                sb.Append("<priority>");
                sb.Append(priorty);
                sb.Append("</priority>");
                sb.Append("</url>");
            }
            sb.Append("</urlset>");
            return sb.ToString();
        }

        public bool DeleteFile(string path)
        {
            try
            {
                //if (File.Exists(path + @"\sitemap.xml"))
                //    File.Delete(path + @"\sitemap.xml");

                string[] pat = Directory.GetFiles(path+@"\sitemap\");
                foreach (string p in pat)
                    if (File.Exists(p))
                        File.Delete(p.ToString());
                return true;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }
        }
    }
}

No comments:

Post a Comment