ASP.NET生成Google網(wǎng)站地圖的代碼
更新時間:2009年08月16日 13:10:39 作者:
生成google網(wǎng)站地圖的實現(xiàn)代碼,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
/// <summary>
/// 生成google網(wǎng)站地圖
/// </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");
//遍歷掃描網(wǎng)站所有文件
showfiles(RootDirectory, Writer);
Writer.WriteEndElement();
Writer.WriteEndDocument();
Writer.Close();
return true;
}
catch (Exception err)
{
return false;
}
}
//遍歷掃描網(wǎng)站所有文件
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("http://", "/").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:經(jīng)常,hourly:小時,daily:天,weekly:周,monthly:月,yearly:年
Writer.WriteEndElement();
Writer.WriteStartElement("priority");
Writer.WriteString("0.8");//相對于其他頁面的優(yōu)先權(quán),此值定于0.0 - 1.0之間
Writer.WriteEndElement();
Writer.WriteEndElement();
}
foreach (DirectoryInfo d in dir.GetDirectories())
{
showfiles(d.FullName, Writer);
}
}
catch (Exception) { }
}
相關(guān)文章
ASP.NET Core項目結(jié)構(gòu)教程(4)
這篇文章主要為大家詳細(xì)介紹了ASP.NET Core項目結(jié)構(gòu),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
ASP.NET漢字轉(zhuǎn)拼音 - 輸入漢字獲取其拼音的具體實現(xiàn)
這篇文章主要介紹了ASP.NET漢字轉(zhuǎn)拼音 - 輸入漢字獲取其拼音的具體實現(xiàn),需要的朋友可以參考下2014-02-02
asp.net Linq To Xml上手Descendants、Elements遍歷節(jié)點
C#3.0 Vs2008 RTM 本文介紹如何使用 Descendants、Elements快速遍歷XML節(jié)點2009-07-07
ASP.NET Core Zero使用Power Tool工具
這篇文章介紹了ASP.NET Core Zero使用Power Tool工具的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
Entity Framework使用DbModelBuilder API創(chuàng)建表結(jié)構(gòu)
這篇文章介紹了Entity Framework使用DbModelBuilder API創(chuàng)建表結(jié)構(gòu)的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
Visual Studio Debug實戰(zhàn)教程之?dāng)帱c操作
眾所周知斷點對于Visual Studio調(diào)試過程是十分重要的,斷點的設(shè)置也是為了更好的進(jìn)行調(diào)試。下面這篇文章主要給大家介紹了關(guān)于Visual Studio Debug實戰(zhàn)教程之?dāng)帱c操作的相關(guān)資料,需要的朋友可以參考下2018-09-09

