asp.net 读取数据库生成百度sitemap_baidu.xml和谷歌sitemap.xml

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
using System.Data.OleDb;
using System.Web;

namespace IWMS_Plugin
{
    public class setSEXml : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void toBaiDuXml(Object o, EventArgs e)
        {
            toBaiDuXml(getTb());
        }

        private DataRow[] getTb()
        {
            string cmdText = "SELECT articleid,title,classid,dateandtime FROM iwms_news WHERE dateandtime>[@dateandtime] ORDER BY dateandtime DESC ";
            OleDbParameter p = new OleDbParameter("@dateandtime", typeof(System.String));
            p.Value = DateTime.Now.AddDays(-7).ToString();
            OleDbParameter[] ps = new OleDbParameter[] { p };
            DataSet ds = DABase.GetDataSet(cmdText, ps, "Topics");
            return ds.Tables[0].Select();
        }

        public void toGoogleXml(Object o, EventArgs e)
        {
            toGoogleXml(getTb());
        }

        public void toBaiDuXml(DataRow[] rows)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlDeclaration declareation;
            declareation = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
            xmlDoc.AppendChild(declareation);

            XmlElement xeRoot = xmlDoc.CreateElement("document");
            xeRoot.SetAttribute("xmlns:bbs", "http://www.baidu.com/search/bbs_sitemap.xsd");
            xmlDoc.AppendChild(xeRoot);

            XmlNode root = xmlDoc.SelectSingleNode("document");

            XmlElement webSite = xmlDoc.CreateElement("webSite");
            webSite.InnerText = "www.scykw.cn";
            root.AppendChild(webSite);

            XmlElement webMaster = xmlDoc.CreateElement("webMaster");
            webMaster.InnerText = "snwxd@126.com";
            root.AppendChild(webMaster);

            XmlElement updatePeri = xmlDoc.CreateElement("updatePeri");
            updatePeri.InnerText = "168";
            root.AppendChild(updatePeri);

            XmlElement updatetime = xmlDoc.CreateElement("updatetime");
            updatetime.InnerText = DateTime.Now.ToString();
            root.AppendChild(updatetime);

            XmlElement version = xmlDoc.CreateElement("version");
            version.InnerText = "Iwms Plugin by 蛰伏的虫";
            root.AppendChild(version);

            foreach (DataRow row in rows)
            {
                string articleid=row["articleid"].ToString();
                string title = row["title"].ToString();
                string classid = row["classid"].ToString();
                string dateandtime = row["dateandtime"].ToString();

                XmlElement item = xmlDoc.CreateElement("item");
                root.AppendChild(item);

                XmlElement link = xmlDoc.CreateElement("link");
                link.InnerText = String.Format("http://{0}/Show.aspx?id={1}&cid={2}", "www.scykw.cn",articleid, classid);
                item.AppendChild(link);
                

                XmlElement xml_title = xmlDoc.CreateElement("title");
                xml_title.InnerText = title;
                item.AppendChild(xml_title);

                XmlElement pubDate = xmlDoc.CreateElement("pubDate");
                pubDate.InnerText = dateandtime;
                item.AppendChild(pubDate);
            }

            xmlDoc.Save(Server.MapPath("~/sitemap_baidu.xml"));
        }

        private void toGoogleXml(DataRow[] rows)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlDeclaration declareation;
            declareation = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
            xmlDoc.AppendChild(declareation);

            XmlElement xeRoot = xmlDoc.CreateElement("urlset");
            xeRoot.SetAttribute("xmlns", "http://www.google.com/schemas/sitemap/0.84");
            xmlDoc.AppendChild(xeRoot);

            XmlNode root = xmlDoc.SelectSingleNode("urlset");

            XmlElement url = xmlDoc.CreateElement("url");
            root.AppendChild(url);

            XmlElement loc = xmlDoc.CreateElement("loc");
            loc.InnerText = String.Format("http://{0}/", "www.scykw.cn");
            url.AppendChild(loc);

            XmlElement lastmod = xmlDoc.CreateElement("lastmod");
            lastmod.InnerText = DateTime.Now.ToString();
            url.AppendChild(lastmod);

            XmlElement changefreq = xmlDoc.CreateElement("changefreq");
            changefreq.InnerText = "always";
            url.AppendChild(changefreq);

            XmlElement priority = xmlDoc.CreateElement("priority");
            priority.InnerText = "1.0" ;
            url.AppendChild(priority);

            foreach (DataRow row in rows)
            {
                string articleid = row["articleid"].ToString();
                string title = row["title"].ToString();
                string classid = row["classid"].ToString();
                string dateandtime = row["dateandtime"].ToString();

                XmlElement xurl = xmlDoc.CreateElement("url");
                root.AppendChild(xurl);

                XmlElement xloc = xmlDoc.CreateElement("loc");
                xloc.InnerText = String.Format("http://{0}/Show.aspx?id={1}&cid={2}", "www.scykw.cn", articleid, classid);
                xurl.AppendChild(xloc);

                XmlElement xlastmod = xmlDoc.CreateElement("lastmod");
                xlastmod.InnerText = dateandtime;
                xurl.AppendChild(xlastmod);

                XmlElement xchangefreq = xmlDoc.CreateElement("changefreq");
                xchangefreq.InnerText = "daily";
                xurl.AppendChild(xchangefreq);

                XmlElement xpriority = xmlDoc.CreateElement("priority");
                xpriority.InnerText = "1.0";
                xurl.AppendChild(xpriority);
            }


            xmlDoc.Save(Server.MapPath("~/sitemap.xml"));
        }
    }
}
posted @ 2009-06-29 17:41  接云网络  阅读(1232)  评论(0编辑  收藏  举报