Click here to Skip to main content
Click here to Skip to main content

Create ASP.NET GoogleSiteMap Using XmlWriter

, 29 Aug 2008
Rate this:
Please Sign up or sign in to vote.
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.

<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>
required Encapsulates the file, and references the current protocol standard.
<url>
required Parent tag for each URL entry. The remaining tags are children of this tag.
<loc>
required URL 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>
optional The 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.

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)

About the Author

Ali Delshad
Web Developer Faradade Co
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
http://faradade.com
http://how2learnasp.net

Comments and Discussions

 
Questionhow to eliminate specific URL ? Pinmemberbabakmb2-Oct-13 4:30 
GeneralMight find this useful Pinmemberekul246-Oct-10 19:38 
GeneralRe: Might find this useful PingroupPaul @ The Computer Station8-May-12 7:10 
GeneralThanks PinmemberMohammad Rastkar3-Nov-08 18:55 
Questiondid I miss a structure update? Pinmembervegeta4ss3-Sep-08 4:59 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 30 Aug 2008
Article Copyright 2008 by Ali Delshad
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid