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

2016-06-11

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml;
using KNet.DBUtility;

public partial class sitemap : System.Web.UI.Page
{
   //设置域名
    private string mywebsite = "www.ktonsoft.com";

    protected void Page_Load(object sender, EventArgs e)
    {}
    /// <summary>
    /// 生成 SiteMap 时数据源
    /// </summary>
    private DataSet GetList(string strWhere, string DBtable)
    {
        StringBuilder strSql = new StringBuilder();
        strSql.Append("select * ");
        strSql.Append(" FROM " + DBtable + " ");
        if (strWhere.Trim() != "")
        {
            strSql.Append(" where " + strWhere);
        }
        return DbHelperSQL.Query(strSql.ToString());
    }

    /// <summary>
    /// baidu xml
    /// </summary>
    /// <param name="rows"></param>
    private void toBaiDuXml(DataSet ds)
    {
        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 = mywebsite;
        root.AppendChild(webSite);

        XmlElement webMaster = xmlDoc.CreateElement("webMaster");
        webMaster.InnerText = "youdme@163.com";
        root.AppendChild(webMaster);

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

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

        XmlElement version = xmlDoc.CreateElement("version");
        version.InnerText = "ktonsoft.com v1.0";
        root.AppendChild(version);


        for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            DataRowView row = ds.Tables[0].DefaultView[i];

            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}", mywebsite, 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"));
    }
    /// <summary>
    /// google xml
    /// </summary>
    /// <param name="rows"></param>
    private void toGoogleXml(DataSet ds)
    {
        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}/", mywebsite);
        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);

        for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            DataRowView row = ds.Tables[0].DefaultView[i];

            string CustomerID = row["CustomerID"].ToString();
            string title = row["ContactTitle"].ToString();
            string dateandtime = DateTime.Now.ToString();

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

            XmlElement xloc = xmlDoc.CreateElement("loc");
            xloc.InnerText = String.Format("http://{0}/Show.aspx?id={1}", mywebsite, CustomerID);
            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"));
    }

    /// <summary>
    /// googel
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.toGoogleXml(GetList("", "Customers"));
    }
}