控制台程序 自动生成Google sitemaps地图文件

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using System.Windows.Forms;

using System.IO;



namespace SiteMaps

{

   
class Program

    {

       
static void Main( string [] args)

        {

Console.WriteLine(
" SQL语句 " );

           
string sql = null ;

            sql
= Console.ReadLine();



           
// Console.WriteLine("请输入文件名(路径)");

           
// string file = null;

           
// file = Console.ReadLine();

            string file = Application.StartupPath + " / " + DateTime.Now.ToString( " yyyyMMddhhmmss " ) + " .xml " ;

            StreamWriter sw
= new StreamWriter(file);

            sw.WriteLine(
" <?xml version=\"1.0\" encoding=\"UTF-8\"?> " );

            sw.WriteLine(
" <urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\"> " );

            sw.WriteLine(
" <url> " );

            sw.WriteLine(
" <loc>{0}</loc> " , ConfigurationManager.AppSettings[ " domain " ]);

            sw.WriteLine(
" <lastmod>{0}</lastmod> " , DateTime.Now.ToString( " yyyy-MM-dd " ));

            sw.WriteLine(
" <changefreq>daily</changefreq> " );

            sw.WriteLine(
" <priority>1.0</priority> " );

            sw.WriteLine(
" </url> " );



            Console.WriteLine(
" 开始生成…… " );



           
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[ " db " ].ConnectionString))

            {

                SqlCommand cmd
= new SqlCommand(sql, conn);

                conn.Open();

               
using (SqlDataReader dr = cmd.ExecuteReader())

                {

                   
while (dr.Read())

                    {

                       
string url = dr[ 0 ].ToString();

                        sw.WriteLine(
" <url> " );

                        sw.WriteLine(
" <loc>{0}</loc> " , url);

                        sw.WriteLine(
" <lastmod>{0}</lastmod> " , DateTime.Now.ToString( " yyyy-MM-dd " ));

                        sw.WriteLine(
" <changefreq>daily</changefreq> " );

                        sw.WriteLine(
" </url> " );

                        Console.WriteLine(url);

                    }

                    dr.Close();

                }

                conn.Close();

            }



            sw.WriteLine(
" </urlset> " );

            sw.Close();

            Console.WriteLine(
" 生成成功!\a " );

        }

    }

}

你可能感兴趣的:(Google)