放逐忧伤

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
namespace SiteMap
{
    class Program
    {

        static void Main(string[] args)
        {

            string filePath="E:\\SiteMap";
            //List<string> CategoryCodes = RequestData.GetCategoryCodes();
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            CreateBigSiteMap(filePath);
            CreateCategorySiteMap(filePath);
            CreateFilesSiteMap(filePath);

        }

        static void CreateBigSiteMap(string filePath)
        {
            List<string> FileIDs = RequestData.GetFileIDs();
            int count = (FileIDs.Count % 30000 == 0) ? FileIDs.Count / 30000 : (FileIDs.Count / 30000 + 1);
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?><sitemapindex></sitemapindex>");
            XmlElement rootElement = xmldoc.DocumentElement;

            for (int i = 0; i < count; i++)
            {
                if (i == 0)
                {
                    XmlNode rootNode = xmldoc.CreateNode("element", "sitemap", "");
                    XmlNode xmlnode = xmldoc.CreateNode("element", "loc", "");
                    xmlnode.InnerText = "http://xiazai.dichan.com/sitemap/sitemap_list.xml";
                    rootNode.AppendChild(xmlnode);
                    xmlnode = xmldoc.CreateNode("element", "lastmod", "");
                    xmlnode.InnerText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    rootNode.AppendChild(xmlnode);
                    rootElement.AppendChild(rootNode);
                }
                else
                {
                    XmlNode rootNode = xmldoc.CreateNode("element", "sitemap", "");
                    XmlNode xmlnode = xmldoc.CreateNode("element", "loc", "");
                    xmlnode.InnerText = "http://xiazai.dichan.com/sitemap/files-"+i.ToString()+".xml";
                    rootNode.AppendChild(xmlnode);
                    xmlnode = xmldoc.CreateNode("element", "lastmod", "");
                    xmlnode.InnerText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    rootNode.AppendChild(xmlnode);
                    rootElement.AppendChild(rootNode);
                }
            }
            XmlAttribute attr = xmldoc.CreateAttribute("xmlns");
            attr.Value = "http://www.sitemaps.org/schemas/sitemap/0.9";
            rootElement.Attributes.Append(attr);
            xmldoc.Save(filePath + "\\sitemap.xml");
            Console.WriteLine("文件XML生成完成!");

        
        }
        static void CreateFilesSiteMap(string filePath)
        {
            List<string> FileIDs = RequestData.GetFileIDs();
            int count = (FileIDs.Count % 30000 == 0) ? FileIDs.Count / 30000 : (FileIDs.Count / 30000 + 1);
            Random rd = new Random();
            float priorityvalue;

            int eachcount = 30000;
            for (int i = 0; i < count; i++)
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?><urlset></urlset>");
                XmlElement rootElement = xmldoc.DocumentElement;
                for (int j = eachcount * i; j < eachcount * (i + 1); j++)
                {
                    if (j > (FileIDs.Count - 1))
                    {
                        break;
                    }
                    XmlNode rootNode = xmldoc.CreateNode("element", "url", "");
                    XmlNode xmlnode = xmldoc.CreateNode("element", "loc", "");
                    xmlnode.InnerText = "http://xiazai.dichan.com/show-" + FileIDs[j].ToString() + ".html";
                    rootNode.AppendChild(xmlnode);
                    xmlnode = xmldoc.CreateNode("element", "changefreq", "");
                    xmlnode.InnerText = "mothly";
                    rootNode.AppendChild(xmlnode);
                    xmlnode = xmldoc.CreateNode("element", "priority", "");
                    priorityvalue = rd.Next(1, 10);
                    xmlnode.InnerText = (priorityvalue / 10).ToString("0.0");
                    rootNode.AppendChild(xmlnode);
                    rootElement.AppendChild(rootNode);
                }
                xmldoc.Save(filePath + "\\files-" + (i+1).ToString() + ".xml");
                Console.WriteLine("files-" + i.ToString() + "XML生成完成!");
            }
              
        }
        static void CreateCategorySiteMap(string filePath)
        {

            List<string> CategoryCodes = RequestData.GetCategoryCodes();
       
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?><urlset></urlset>");
            XmlElement rootElement = xmldoc.DocumentElement;
            Random rd = new Random();
            float priorityvalue ;
            foreach (string category in CategoryCodes)
            {
                XmlNode rootNode = xmldoc.CreateNode("element", "url", "");
                XmlNode xmlnode = xmldoc.CreateNode("element", "loc", "");
                xmlnode.InnerText = "http://xiazai.dichan.com/list-" + category + ".html";
                rootNode.AppendChild(xmlnode);
                xmlnode = xmldoc.CreateNode("element", "changefreq", "");
                xmlnode.InnerText = "mothly";
                rootNode.AppendChild(xmlnode);
                xmlnode = xmldoc.CreateNode("element", "priority", "");
                priorityvalue = rd.Next(1, 10);

                xmlnode.InnerText = (priorityvalue / 10).ToString("0.0");
                rootNode.AppendChild(xmlnode);
                rootElement.AppendChild(rootNode);
            }

      
            xmldoc.Save(filePath + "\\sitemap_list.xml");
            Console.WriteLine("类别XML生成完成!");
        }
    }
}
posted on 2011-03-29 13:43  放逐忧伤  阅读(280)  评论(0)    收藏  举报