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

來源:互聯網
上載者:User

複製代碼 代碼如下:/// <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) { }
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.