ASP.Net: Create Google compliant dynamic XML sitemap in asp.net

Google SiteMaps are an important tool for website developers, webmasters.  A Google Sitemap is an XML file which instructs the Google crawler which URLs in your site to visit, and allows you to tell the crawler how often pages are updated, The lastMod attribute tells Google when the page was last modified, the changefreq attribute tells Google how often the page changesand you can also place a relative priority on the pages within the site. The instructions Google gives is to code the XML file by hand, but of course, with a dynamic website, you don't want to do that.

Google publish extremely helpful guidelines for site owners (are they still called Web Masters??), including details on the specification of the sitemap format

Let's show a sample Sitemap.xml file that is fully Google compliant .

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                              xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
  <url>
    <loc>http://www.infoa2z.com/asp.net/get-the-all-files-with-their-name,-size-from-a-folder-asp.net</loc>
    <lastmod>2012-12-12</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>http://www.infoa2z.com/sqlserver/count-duplicate-records-from-table-in-sql-server</loc>
    <lastmod>2012-12-12</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.8</priority>
  </url>
</urlset>

Sample code in UpdateSitemap.asp.cs file

Namespace used

using System.Xml;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

  protected void btnsubmit_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.ContentType = "text/xml";
        using (XmlTextWriter writer = new XmlTextWriter(Server.MapPath("../uploadfiles/sitemap.xml"), Encoding.UTF8))
        {

            writer.WriteStartDocument();

            writer.WriteStartElement("urlset");

            writer.WriteAttributeString("xmlns", "http://www.google.com/schemas/sitemap/0.84");
            writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            writer.WriteAttributeString("xsi:schemaLocation", "http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd");


            SqlDataAdapter adp1 = new SqlDataAdapter("Select A.Title,lower((select name from Articles AC where AC.CategoryId=A.CategoryId)+'/'+url) as 'URL' from Articles A where A.Isactive=1", config.DbConn);
            DataSet ds = new DataSet();
            adp1.Fill(ds);
              if (ds.Tables[0].Rows.Count > 0)
              {
                
                    for (int LIntCtr = 0; LIntCtr <= ds.Tables[0].Rows.Count - 1; LIntCtr++)
                    {
                        writer.WriteStartElement("url");

                        writer.WriteElementString("loc", "http://www.infoa2z.com/" + ds.Tables[0].Rows[LIntCtr]["url"].ToString().ToString() + "");

                            writer.WriteElementString("lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now));

                            writer.WriteElementString("changefreq", "daily");

                            writer.WriteElementString("priority", "0.8");
                            writer.WriteEndElement();
                    }
              }
              writer.WriteEndElement();
                     writer.Flush();   

  }
    }

Related Alrticles

Add Your Business in Free Listing


FREE!!! Registration