Click here to Skip to main content
13,513,739 members
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 29 Aug 2008

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#.


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.


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

<urlset xmlns="">

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.
requiredEncapsulates the file, and references the current protocol standard.
requiredParent tag for each URL entry. The remaining tags are children of this tag.
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.
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.

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.


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.WriteStartElement("urlset", "");

        WriteTag("1", "Daily", "", writer);
        WriteTag("0.6", "Yearly", "", writer);
        WriteTag("0.8", "Monthly", "", writer);





    private void WriteTag(string Priority, string freq, 
            string Navigation, XmlWriter MyWriter)






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.


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)

You may also be interested in...


Comments and Discussions

Questionthx! Pin
Kadir Çimen17-Mar-15 9:14
memberKadir Çimen17-Mar-15 9:14 
Questionhow to eliminate specific URL ? Pin
babakmb2-Oct-13 4:30
memberbabakmb2-Oct-13 4:30 
GeneralMight find this useful Pin
ekul246-Oct-10 19:38
memberekul246-Oct-10 19:38 
GeneralRe: Might find this useful Pin
Paul @ The Computer Station8-May-12 7:10
groupPaul @ The Computer Station8-May-12 7:10 
GeneralThanks Pin
Mohammad Rastkar3-Nov-08 18:55
memberMohammad Rastkar3-Nov-08 18:55 
Questiondid I miss a structure update? Pin
vegeta4ss3-Sep-08 4:59
membervegeta4ss3-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180417.1 | Last Updated 30 Aug 2008
Article Copyright 2008 by Ali Delshad
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid