1 /// <summary>
2 /// 生成google網站地圖
3 /// </summary>
4 /// <returns></returns>
5 public static bool BuildGoogleSitemap()
6 {
7 try
8 {
9 string RootDirectory = AppDomain.CurrentDomain.BaseDirectory;
10 XmlTextWriter Writer = new XmlTextWriter(HttpContext.Current.Server.MapPath("~/GoogleSitemaps.xml"), Encoding.UTF8);
11 Writer.Formatting = Formatting.Indented;
12 Writer.WriteStartDocument();
13 Writer.WriteStartElement("urlset", "http://www.google.com/schemas/sitemap/0.84");
14 //遍歷掃描網站所有文件
15 showfiles(RootDirectory, Writer);
16
17 Writer.WriteEndElement();
18 Writer.WriteEndDocument();
19 Writer.Close();
20 return true;
21
22 }
23 catch (Exception err)
24 {
25 return false;
26 }
27 }
28
29 //遍歷掃描網站所有文件
30 static void showfiles(string dirpath, XmlTextWriter Writer)
31 {
32 try
33 {
34 DirectoryInfo dir = new DirectoryInfo(dirpath);
35 foreach (FileInfo f in dir.GetFiles())
36 {
37 string path = dir.FullName.Replace(AppDomain.CurrentDomain.BaseDirectory, "");//文件相對目錄
38 //HttpContext.Current.Response.Write(AppDomain.CurrentDomain.BaseDirectory + "**********" + dir.FullName + "<br>");
39 Writer.WriteStartElement("url");
40
41 Writer.WriteStartElement("loc");
42 StringBuilder sb = new StringBuilder("/" + path + "/" + f.Name);
43 sb.Replace("//", "/").Replace(@"/", "/");
44 Writer.WriteString(ConfigurationManager.AppSettings["WebSiteUrl"].ToString() + sb.ToString());
45 Writer.WriteEndElement();
46
47 Writer.WriteStartElement("lastmod");
48 Writer.WriteString(string.Format("{0:yyyy-MM-dd}", f.LastWriteTime));
49 Writer.WriteEndElement();
50
51 Writer.WriteStartElement("changefreq");
52 Writer.WriteString("always");//更新頻率:always:經常,hourly:小時,daily:天,weekly:周,monthly:月,yearly:年
53 Writer.WriteEndElement();
54
55 Writer.WriteStartElement("priority");
56 Writer.WriteString("0.8");//相對於其他頁面的優先權,此值定於0.0 - 1.0之間
57 Writer.WriteEndElement();
58
59 Writer.WriteEndElement();
60 }
61 foreach (DirectoryInfo d in dir.GetDirectories())
62 {
63 showfiles(d.FullName, Writer);
64 }
65 }
66 catch (Exception) { }
67 }
68
ASP.NET生成Google站點地圖
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.