Click here to Skip to main content
15,881,173 members
Articles / Web Development / ASP.NET

Create ASP.NET GoogleSiteMap Using XmlWriter

Rate me:
Please Sign up or sign in to vote.
3.30/5 (5 votes)
29 Aug 2008CPOL4 min read 35.5K   442   17   6
Create a Google Sitemap dynamically with ASP.NET and C#.

Introduction

The Sitemap protocol allows you to inform search engines about URLs on your websites that are available for crawling. In its simplest form, a Sitemap that uses the Sitemap protocol is an XML file that lists URLs for a site. The protocol was written to be highly scalable so it can accommodate sites of any size. It also enables webmasters to include additional information about each URL (when it was last updated; how often it changes; how important it is in relation to other URLs in the site) so that search engines can more intelligently crawl the site.

Background

A sample Sitemap that contains just one URL and uses all optional tags is shown below. The optional tags are in italics.

XML
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://www.example.com/</loc>
    <lastmod>2005-01-01</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
</urlset>

The Sitemap must:

  • Begin with an opening <urlset> tag and end with a closing </urlset> tag.
  • Include a <url> entry for each URL as a parent XML tag.
  • Include a <loc> child entry for each <url> parent tag.
<urlset>
requiredEncapsulates the file, and references the current protocol standard.
<url>
requiredParent tag for each URL entry. The remaining tags are children of this tag.
<loc>
requiredURL of the page. This URL must begin with the protocol (such as HTTP) and end with a trailing slash, if your web server requires it. This value must be less than 2048 characters.
<lastmod>
optionalThe date of last modification of the file. This date should be in W3C Datetime format. This format allows you to omit the time portion, if desired, and use YYYY-MM-DD.
<changefreq>
optional

How frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page. Valid values are:

  • always
  • hourly
  • daily
  • weekly
  • monthly
  • yearly
  • never

The value "always" should be used to describe documents that change each time they are accessed. The value "never" should be used to describe archived URLs.

Please note that the value of this tag is considered a hint and not a command. Even though search engine crawlers consider this information when making decisions, they may crawl pages marked "hourly" less frequently than that, and they may crawl pages marked "yearly" more frequently than that. It is also likely that crawlers will periodically crawl pages marked "never" so that they can handle unexpected changes to those pages.

<priority>
optional

The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0. This value has no effect on your pages compared to pages on other sites, and only lets the search engines know which of your pages you deem most important so they can order the crawl of your pages in the way you would most like.

The default priority of a page is 0.5.

Please note that the priority you assign to a page has no influence on the position of your URLs in a search engine's result pages. Search engines use this information when selecting between URLs on the same site, so you can use this tag to increase the likelihood that your more important pages are present in a search index.

Also, please note that assigning a high priority to all of the URLs on your site will not help you. Since the priority is relative, it is only used to select between URLs on your site; the priority of your pages will not be compared to the priority of pages on other sites.

Create Site Map XML File Dynamically

I use the XmlWriter class to write Sitemap's XML tags. Before using the XmlWriter, class you should import the System.Xml namespace with the using keyword .

Then create a void method WriteTag that writes each Google sitemap XML block and call it in the Page Load event code block.

C#
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        XmlWriter writer = XmlWriter.Create(Server.MapPath("GSiteMap.xml"));

        writer.WriteStartDocument();
        writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");

        WriteTag("1", "Daily", "http://www.delshad.ir/default.aspx", writer);
        WriteTag("0.6", "Yearly", "http://www.delshad.ir/Contact.aspx", writer);
        WriteTag("0.8", "Monthly", "http://www.delshad.ir/About.aspx", writer);

        writer.WriteEndDocument();

        writer.Close();

        Response.Redirect("GSiteMap.xml");

    }

    private void WriteTag(string Priority, string freq, 
            string Navigation, XmlWriter MyWriter)
    {
        MyWriter.WriteStartElement("url");

        MyWriter.WriteStartElement("loc");
        MyWriter.WriteValue(Navigation);
        MyWriter.WriteEndElement();

        MyWriter.WriteStartElement("lastmod");
        MyWriter.WriteValue(DateTime.Now.ToShortDateString());
        MyWriter.WriteEndElement();

        MyWriter.WriteStartElement("changefreq");
        MyWriter.WriteValue(freq);
        MyWriter.WriteEndElement();

        MyWriter.WriteStartElement("priority");
        MyWriter.WriteValue(Priority);
        MyWriter.WriteEndElement();

        MyWriter.WriteEndElement();
    }
}

You can call the WriteTag method in a loop that reads data from the database. Note that you should set the write permission for your XML file (GSiteMap.xml). After you finish up writing the ASP.NET code and uploading it, you should add your site map in Google Webmaster Tools.

If you do not have any Google account, first create an account then go to Google Webmaster Tools.

After logging in Webmaster Tools, add your website URL in the dashboard, then select SiteMap>Add SiteMap, then choose "Add General Web SiteMap" from the dropdown list. Type your XML SiteMap file in the SiteMap URL textbox (for example: GSiteMap.xml), then click Add. After adding your SiteMap, you should verify it. To verify your SiteMap, go to Dashboard and select Verify from the SiteMap's list.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Faradade Co
Iran (Islamic Republic of) Iran (Islamic Republic of)
http://faradade.com
http://how2learnasp.net

Comments and Discussions

 
Questionthx! Pin
Kadir Çimen17-Mar-15 9:14
Kadir Çimen17-Mar-15 9:14 
Questionhow to eliminate specific URL ? Pin
babakmb2-Oct-13 4:30
babakmb2-Oct-13 4:30 
GeneralMight find this useful Pin
ekul246-Oct-10 19:38
ekul246-Oct-10 19:38 
GeneralRe: Might find this useful Pin
Paul_Williams8-May-12 7:10
Paul_Williams8-May-12 7:10 
GeneralThanks Pin
Mohammad Rastkar3-Nov-08 18:55
Mohammad Rastkar3-Nov-08 18:55 
Questiondid I miss a structure update? Pin
vegeta4ss3-Sep-08 4:59
vegeta4ss3-Sep-08 4:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.