自动生成Google sitemaps地图文件
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
using
System.Data;
5
using
System.Data.SqlClient;
6
using
System.Configuration;
7
using
System.Windows.Forms;
8
using
System.IO;
9
10
namespace
SiteMaps
11
{
12
class Program
13
{
14
static void Main(string[] args)
15
{
16
Console.WriteLine("请输入SQL");
17
string sql = null;
18
sql = Console.ReadLine();
19
20
//Console.WriteLine("请输入文件名(路径)");
21
//string file = null;
22
//file = Console.ReadLine();
23
string file = Application.StartupPath + "/" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xml";
24
StreamWriter sw = new StreamWriter(file);
25
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
26
sw.WriteLine("<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\">");
27
sw.WriteLine("<url>");
28
sw.WriteLine("<loc>{0}</loc>", ConfigurationManager.AppSettings["domain"]);
29
sw.WriteLine("<lastmod>{0}</lastmod>", DateTime.Now.ToString("yyyy-MM-dd"));
30
sw.WriteLine("<changefreq>daily</changefreq>");
31
sw.WriteLine("<priority>1.0</priority>");
32
sw.WriteLine("</url>");
33
34
Console.WriteLine("开始生成");
35
36
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString))
37
{
38
SqlCommand cmd = new SqlCommand(sql, conn);
39
conn.Open();
40
using (SqlDataReader dr = cmd.ExecuteReader())
41
{
42
while (dr.Read())
43
{
44
string url = dr[0].ToString();
45
sw.WriteLine("<url>");
46
sw.WriteLine("<loc>{0}</loc>", url);
47
sw.WriteLine("<lastmod>{0}</lastmod>", DateTime.Now.ToString("yyyy-MM-dd"));
48
sw.WriteLine("<changefreq>daily</changefreq>");
49
sw.WriteLine("</url>");
50
Console.WriteLine(url);
51
}
52
dr.Close();
53
}
54
conn.Close();
55
}
56
57
sw.WriteLine("</urlset>");
58
sw.Close();
59
Console.WriteLine("生成成功!\a");
60
}
61
}
62
}
63

2

3

4

5

6

7

8

9

10

11



12

13



14

15



16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37



38

39

40

41



42

43



44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63
