Build a search engine sitemap in C #

Source: Internet
Author: User
Tags url example

Sitemaps are XML files that web masters can create to let search engines know what pages to index and how frequently to check for changes on each page. the XML format of the sitemap file is detailed on sitemaps.org. here is a sample of a sitemap with a singleURL.

 
 
  
   
    http://example.com
   
   
    daily
   
  
 

My sample differs from the one on sitemaps.org. The more defined namespace in my example will validate on Google, Yahoo! And with ask.com. At the time of this writing, theirs doesn't.LocAndChangefreqAre required,PriorityAndLastmodAre optional.
My advice with the search engines is treat them like a passport agent. say only what is required and nothing else or you cocould find yourself sent to the end of the line. once you 've determined what URLs will be on the sitemap, the only determinis definingChangefreqOf each page. One strategy might be to set the home pageDaily, Section pagesWeeklyAnd content pagesMonthly. If your home page has stock tickers or sports scores, you cocould setChangefreqToAlwaysOrHourly.
Google prefers the sitemap file to be in the root folder and every example on their site names the fileSitemap. xml. What Google wants, Google gets.

Additional namespaces
using System.IO;using System.Xml;
Sample Code

You will needWriteAccess to the sitemap. XML file. since you don't want to give your entire root folder write access, my advice is to create a dummy sitemap. XML, place it into the root folder and then set write access to that file. addingTry... catchTo the code below will alert you if thatWriteAccess is not there.

string SiteMapFile = @"~/sitemap.xml";string xmlFile = Server.MapPath(SiteMapFile);XmlTextWriter writer = new XmlTextWriter(xmlFile, System.Text.Encoding.UTF8);writer.Formatting = Formatting.Indented;writer.WriteStartDocument();writer.WriteStartElement("urlset");writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");writer.WriteAttributeString("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");// Add Home Pagewriter.WriteStartElement("url");writer.WriteElementString("loc", "http://example.com");writer.WriteElementString("changefreq", "daily");writer.WriteEndElement(); // url// Add Sections and ArticlesSqlConnection con = new SqlConnection(connectionString);string sql = @"SELECT url, 'weekly' as changefreq FROM Section UNION SELECT url, 'monthly' as changefreq FROM Articles ";SqlCommand cmd = new SqlCommand(sql, con);cmd.CommandType = CommandType.Text;try{    con.Open();    SqlDataReader reader = cmd.ExecuteReader();    while (reader.Read())    {        string loc = "http://example.com" + reader["URL"].ToString();        string changefreq = reader["changefreq"].ToString();        writer.WriteStartElement("url");        writer.WriteElementString("loc", loc);        writer.WriteElementString("changefreq", changefreq);        writer.WriteEndElement(); // url    }    reader.Close();}catch (SqlException err){    throw new ApplicationException("Data Error (Sections):" + err.Message);}finally{    con.Close();}writer.WriteEndElement();// urlset        writer.Close();
Validate your sitemap

Once you 've confirmed you have a good looking sitemap. XML file in the root folder of your web site and it contains all the pages you want indexed by the search engines, it is now time to validate it. XML-Sitemaps.com has a sitemap validator that you can test out your new sitemap. once it's fine, move to the next step.

Update your robots.txt File

Add the location of your sitemap in your robots.txt file.
Sitemap: http://example.com/sitemap.xml

Google webmaster

Google has a suite of tools for managing your relationship between your web sites and them. they call this suite Google webmaster central. it is here that you will register your web sites with validation files. once they 've established you are the webmaster of your site, they will present you will a screen to submit your sitemap. you will need a Google account for this process. if you don't have one, follow the linkCreate a Google account.

Yahoo! Site explorer

Yahoo! Has a similar setup which is called Yahoo! Site explorer. Using your Yahoo! ID, you will go through the same process of registering your web sites. and once that process has been completed, you can then submit your sitemap. XML file. don't have a Yahoo ID? Get one.

Ask.com

Ask.com doesn't require any accounts or site validation. Just ping their server with the location of your sitemap. xml file modeled after the URL example below. Of course replaceExample.comWith your domain name.
Http://submissions.ask.com/ping? Sitemap = http % 3A // example.com/sitemap.xml

Monitoring the sitemap crawl

BothYahoo! Site explorerAndGoogle webmaster toolsHave reports that provide updated status on the success and failure of the sitemap crawl. ask.com to my knowledge doesn't have any such tools. and I cocould locate a sitemap submission tool at all for msn.com.

Using an HTTP handler

This example uses the file system. Another option is to use an HTTP handler to deliver the sitemap. xml.

Final Word

A friend of mine with a low traffic site saw his page views double after adding a sitemap. If one page of code can potentially double your page views, then it is worth pursuing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.