YongGe

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
自动生成Google sitemaps地图文件
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.Data;
 5using System.Data.SqlClient;
 6using System.Configuration;
 7using System.Windows.Forms;
 8using System.IO;
 9
10namespace 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
posted on 2008-06-09 15:48  YongGe  阅读(526)  评论(0编辑  收藏  举报