C#生成sitemap站點地圖的方法
Sitemaps是Google的一個和網(wǎng)站管理員相關(guān)的工具,有點象BLOG的RSS功能,是一個方便自己的服務(wù),如果大家都采用了這種方式提交自己的更新的話,Google就再也不用派出那么多爬蟲辛辛苦苦的到處亂竄了,任何一個站點,只要有更新,便會自動“通知”Google,方便Google進行索引。
好像最近BAIDU也開始支持XML格式的sitemap的站點地圖了。
目前網(wǎng)絡(luò)上有很多免費的生成sitemap站點地圖的工具,使用起來也比較方便。其原理就是抓取你指定的頁面,獲取頁面上所有的鏈接,根據(jù)這些鏈接生成一個xml格式的sitemap站點地圖文件。
但是這樣做的缺點就是只能生成頁面上有鏈接的sitemap站點地圖,同時,站點地圖需要手工上傳到服務(wù)器才能使用。
下面就提供一種通過C#自動生成sitemap站點地圖的方法,可以直接通過網(wǎng)站系統(tǒng)生成在服務(wù)器目錄,而且根據(jù)自己的需求任意設(shè)置需要顯示在sitemap站點地圖中的鏈接。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Voodoo.other.SEO
{
/// <summary>
/// 生成站點地圖sitemap
/// (c) http://aizr.net
/// </summary>
public class SiteMap
{
public List<PageInfo> url
{
get;
set;
}
/// <summary>
/// 生成SiteMap字符串
/// </summary>
/// <returns></returns>
public string GenerateSiteMapString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(“<?xml version=\”1.0\” encoding=\”UTF-8\”?> “);
sb.AppendLine(“<urlset xmlns=\”http://www.sitemaps.org/schemas/sitemap/0.9\”> “);
foreach (PageInfo pi in url)
{
sb.AppendLine(“<url>”);
sb.AppendLine(string.Format(“<loc>{0}</loc>”,pi.loc));
sb.AppendLine(string.Format(“<lastmod>{0}</lastmod> “, pi.lastmod.ToString(“yyyy-MM-dd”)));
sb.AppendLine(string.Format(“<changefreq>{0}</changefreq> “, pi.changefreq));
sb.AppendLine(string.Format(“<priority>{0}</priority> “,pi.priority));
sb.AppendLine(“</url>”);
}
sb.AppendLine(“</urlset>”);
return sb.ToString();
}
/// <summary>
/// 保存Site文件
/// </summary>
/// <param name=”FilePath”>路徑</param>
public void SaveSiteMap(string FilePath)
{
Voodoo.IO.File.Write(FilePath, GenerateSiteMapString());//保存在指定目錄下
}
}
public class PageInfo
{
/// <summary>
/// 網(wǎng)址
/// </summary>
public string loc { get; set; }
/// <summary>
/// 最后更新時間
/// </summary>
public DateTime lastmod { get; set; }
/// <summary>
/// 更新頻繁程度
/// </summary>
public string changefreq{get;set;}
/// <summary>
/// 優(yōu)先級,0-1
/// </summary>
public string priority { get; set; }
}
}
相關(guān)文章
C#中的靜態(tài)字段double.Epsilon實例詳解
double.Epsilon 是C#中的一個靜態(tài)字段,表示 double 數(shù)據(jù)類型的最小可表示的正數(shù)值,這篇文章主要介紹了C#中的靜態(tài)字段double.Epsilon的相關(guān)知識,需要的朋友可以參考下2024-01-01c# 服務(wù)器上傳木馬監(jiān)控代碼(包含可疑文件)
c# 監(jiān)控服務(wù)器上傳木馬(包含可疑文件)2010-05-05