程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> ASP.NET生成Google網站地圖的代碼

ASP.NET生成Google網站地圖的代碼

編輯:ASP.NET基礎
復制代碼 代碼如下:
/// <summary>
/// 生成google網站地圖
/// </summary>
/// <returns></returns>
public static boolBuildGoogleSitemap()
{
try
{
string RootDirectory = AppDomain.CurrentDomain.BaseDirectory;
XmlTextWriter Writer = new XmlTextWriter(HttpContext.Current.Server.MapPath("~/GoogleSitemaps.xml"), Encoding.GetEncoding("utf-8"));
Writer.Formatting = Formatting.Indented;
Writer.WriteStartDocument();
Writer.WriteStartElement("urlset", "http://www.google.com/schemas/sitemap/0.84");
//遍歷掃描網站所有文件
showfiles(RootDirectory, Writer);
Writer.WriteEndElement();
Writer.WriteEndDocument();
Writer.Close();
return true;
}
catch (Exception err)
{
return false;
}
}
//遍歷掃描網站所有文件
static void showfiles(string dirpath, XmlTextWriter Writer)
{
bool IsRead = true;
string[] NotRead ={ "App_Data", "Bin", "fckeditor", "js", "MyAdmin", "PowerChatRoom" };//排除這些文件夾
foreach (string s in NotRead)
{
string dirname = dirpath.Substring(dirpath.LastIndexOf(@"\") + 1);
if (dirname == s)
{
IsRead = false;
break;
}
}
if (!IsRead)
return;
try
{
DirectoryInfo dir = new DirectoryInfo(dirpath);
foreach (FileInfo f in dir.GetFiles())
{
string path = dir.FullName.Replace(AppDomain.CurrentDomain.BaseDirectory, "");//文件相對目錄
//HttpContext.Current.Response.Write(AppDomain.CurrentDomain.BaseDirectory + "**********" + dir.FullName + "<br>");
Writer.WriteStartElement("url");
Writer.WriteStartElement("loc");
StringBuilder sb = new StringBuilder("/" + path + "/" + f.Name);
sb.Replace("//", "/").Replace(@"\", "/");
Writer.WriteString(ConfigurationManager.AppSettings["WebSiteUrl"].ToString() + sb.ToString());
Writer.WriteEndElement();
Writer.WriteStartElement("lastmod");
Writer.WriteString(string.Format("{0:yyyy-MM-dd}", f.LastWriteTime));
Writer.WriteEndElement();
Writer.WriteStartElement("changefreq");
Writer.WriteString("always");//更新頻率:always:經常,hourly:小時,daily:天,weekly:周,monthly:月,yearly:年
Writer.WriteEndElement();
Writer.WriteStartElement("priority");
Writer.WriteString("0.8");//相對於其他頁面的優先權,此值定於0.0 - 1.0之間
Writer.WriteEndElement();
Writer.WriteEndElement();
}
foreach (DirectoryInfo d in dir.GetDirectories())
{
showfiles(d.FullName, Writer);
}
}
catch (Exception) { }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved