<a target="_blank" href="http://www.cnblogs.com/freeflying/archive/2010/02/19/1669484.html">本系列目錄</a>
網站地圖的作用是讓搜尋引擎盡快的,更多的收錄網站的各個網頁。
這裡我們首先要明白一個基本的原理,搜尋引擎的爬行方式。整個網際網路就像一張縱橫交錯的“網”:網的各個節點就是各個網頁,而各個網頁之間通過url互相連接配接。蜘蛛可以從一個網頁出發,通過該網頁上的url,爬到另一個網頁;再通過另一個網頁上的url,再爬到更多的網頁……,以此類推。但如果是一個新釋出的網站,可能就沒有其他url指向它,那麼它就永遠不會被“爬到”(收錄)。為了解決這個問題,新站可以自己主動向搜尋引擎送出url,申請蜘蛛前來抓取(Google申請網址:),但申請時一般隻會送出一個首頁的url。
和處理重複内容的robots.txt檔案,我們通過.ashx檔案來生成一個基于sitemaps.org的xml格式的網站地圖。網站地圖生成之後,我們就可以向Google等搜尋引擎送出。大量的文章證明,送出網站地圖将極大的提高網站的收錄速度和深度。其他幾乎所有的SEO方法,都有可能效果難以證明、失效甚至帶來副作用,但送出網站地圖除外!
Linq to XML為我們帶來了近乎完美的操作體驗。

WebSite
<%@ WebHandler Language="C#" Class="website" %>
using System;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
public class website : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/xml";
//檔案的聲明資訊,第第三個參數standalone的值yes 表示這個 XML 文檔是自包含的(self-contained)而不依賴于外部所定義的一個 DTD.
XDeclaration declaration = new XDeclaration("1.0", "UTF-8", "yes");
context.Response.Write(declaration);
//XML檔案的命名空間
XNamespace ns = "http://www.google.com/schemas/sitemap/0.84";
XElement siteMap = new XElement(ns + "urlset");
string fixedUrl = "http://www.freeflying.com/article";
string wholeUrl = string.Empty;
//循環取出資料,轉換成XML節點
foreach (var item in Articles.GetArticles())
{
XElement url = new XElement("url");
wholeUrl = string.Format("{0}?id={1}&catelog={2}",fixedUrl,item.ID,item.Catelog);
XElement loc = new XElement("loc", wholeUrl);
XElement lastmod = new XElement("lastmod", item.LastMod.AddDays(-23).ToShortDateString());
XElement changefreq = new XElement("changefreq", item.Frequency);
XElement priority = new XElement("priority", item.Weight);
url.Add(loc, lastmod, changefreq, priority);
siteMap.Add(url);
}
//最後輸出整個xml檔案
context.Response.Write(siteMap);
}
public bool IsReusable {
get {
return false;
}
同樣還将使用到xml技術的還有RSS

RSS
<%@ WebHandler Language="C#" Class="rss" %>
public class rss : IHttpHandler {
context.Response.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
XElement rssFeed = new XElement("rss", new XAttribute("version","2.0"));
XElement channel = new XElement("channel",
new XElement("title", "freeflying"),
new XElement("link", fixedUrl),
new XElement("description","the website for dream flying freely"),
new XElement("pubDate",DateTime.Now.ToString())
);
foreach (var article in Articles.GetArticles())
XElement item = new XElement("item");
XElement title = new XElement("title", article.Title);
wholeUrl = string.Format("{0}?id={1}&catelog={2}", fixedUrl, article.ID, article.Catelog);
XElement link = new XElement("link", wholeUrl);
XElement description = new XElement("description", article.Description);
XElement pubDate = new XElement("pubDate", article.LastMod.ToString());
item.Add(title,link,description,pubDate);
channel.Add(item);
rssFeed.Add(channel);
context.Response.Write(rssFeed);

模拟資料
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
/// <summary>
/// Summary description for Articles
/// </summary>
public class Articles
{
public Articles()
{
//
// TODO: Add constructor logic here
public static List<Article> GetArticles()
return new List<Article>(){
new Article(234, "blog", DateTime.Now.AddDays(-23), Freq.none, 0.8, "asp.net seo", "articles about SEO in asp.net"),
new Article(267, "blog", DateTime.Now.AddDays(-245), Freq.daily, 0.6, "ado.net pro","about the dataset usage"),
new Article(653, "news", DateTime.Now.AddDays(-45), Freq.daily, 1,"CLR via C#","notebook about this book")
};
public class Article
public int ID;
public string Catelog;
public DateTime LastMod;
public double Weight;
public Freq Frequency;
public string Title;
public string Description;
public Article(int id, string catelog, DateTime lastMod, Freq frequency, double weight, string title, string description)
ID = id;
Catelog = catelog;
LastMod = lastMod;
Weight = weight;
Frequency = frequency;
Title = title;
Description = description;
public enum Freq
none = 1,
daily = 2,
weekly = 3,