C#生成sitemap站点地图

    Sitemaps是Google的一个和网站管理员相关的工具,有点象BLOG的RSS功能,是一个方便自己的服务,如果大家都采用了这种方式提交自己的更新的话,Google就再也不用派出那么多爬虫辛辛苦苦的到处乱窜了,任何一个站点,只要有更新,便会自动“通知”Google,方便Google进行索引。

好像最近BAIDU也开始支持XML格式的sitemap的站点地图了。

目前网络上有很多免费的生成sitemap站点地图的工具,使用起来也比较方便。其原理就是抓取你指定的页面,获取页面上所有的链接,根据这些链接生成一个xml格式的sitemap站点地图文件。

但是这样做的缺点就是只能生成页面上有链接的sitemap站点地图,同时,站点地图需要手工上传到服务器才能使用。

下面就提供一种通过C#自动生成sitemap站点地图的方法,可以直接通过网站系统生成在服务器目录,而且根据自己的需求任意设置需要显示在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>
///  网址
///   </summary>
public  string loc {  getset; }

///   <summary>
///  最后更新时间
///   </summary>
public DateTime lastmod {  getset; }

///   <summary>
///  更新频繁程度
///   </summary>
public  string changefreq{ get; set;}

///   <summary>
///  优先级,0-1
///   </summary>
public  string priority {  getset; }
}
}
This entry was posted in  Uncategorized and tagged  SEO搜索引擎优化WebForm技术. Bookmark the  permalink.

转载于:https://www.cnblogs.com/kuibono/archive/2012/02/10/csharp-create-sitemap.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值