January 26, 2012

How to create sitemap runtime in asp.net

How to create sitemap runtime in asp.net

 // begin program abc  
 // declare namespace  
 using System.Xml;  
 using System.Text;  
 using System.Data.SqlClient;  
 //declare class  
 public partial class sitemap : System.Web.UI.Page  
 {  
   SqlConnection cn;  
   protected void Page_Load(object sender, EventArgs e)  
   {  
     try  
     {  
       cn = new SqlConnection("Your connection string");  
       string mydomain = "your domain name";  
       string strSql = "select abc from test";  
       SqlDataAdapter dacontent = new SqlDataAdapter(strSql, cn);  
       DataSet dscontent = new DataSet();  
       dacontent.Fill(dsd, "SiteMap");            
             //Now we are going to create XML file using XMLTextWriter  
       XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);  
       writer.WriteStartDocument();  
       writer.WriteStartElement("urlset", "url");  
       writer.WriteStartElement("url");  
       writer.WriteElementString("loc", "Your Domain Name");  
       writer.WriteElementString("priority", "0.5");  
       writer.WriteEndElement();  
       if (dscontent.Tables[0].Rows.Count > 0)  
       {  
         DataRow dtr;  
         int i = 0;  
         while (i < dscontent.Tables[0].Rows.Count)  
         {  
           dtr = dscontent.Tables[0].Rows[i];  
           writer.WriteStartElement("url");  
           writer.WriteElementString("loc", mydomain + "/" + dtr["abc"].ToString());  
           writer.WriteElementString("priority", "0.5");  
           writer.WriteEndElement();  
           i++;  
         }  
       }  
       writer.WriteEndDocument();  
       writer.Close();  
     }  
     catch (Exception ex)  
     {  
     }  
   }  
 }  
 //end program