C# 网站sitemap.xml生成工具

来源:互联网 发布:网络最好听的歌曲 编辑:程序博客网 时间:2024/04/25 20:03

 我的个人博客网站加冀空间(www.awingnet.com)开通了SEO要制作sitemap.xml文件,自己写很麻烦,于是写了过C#程序。

网站程序是z-blog的ASP版,好久没有用ASP写程序了,还好asp和asx可以共存,于是就用.net Framwork 4写了个小程序。

首先制作选择列表


代码:

 <div class="showdiv">            <p>                要列出文件<br />                <asp:CheckBoxList                    ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">                    <asp:ListItem Selected="True">.html</asp:ListItem>                    <asp:ListItem>.htm</asp:ListItem>                    <asp:ListItem>.asp</asp:ListItem>                    <asp:ListItem>.php</asp:ListItem>                    <asp:ListItem>.aspx</asp:ListItem>                </asp:CheckBoxList>            </p>            <p>                要列出文件夹<br />                <asp:CheckBoxList                    ID="CheckBoxList2" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">                    <asp:ListItem Selected="True">.html</asp:ListItem>                    <asp:ListItem>.htm</asp:ListItem>                    <asp:ListItem>.asp</asp:ListItem>                    <asp:ListItem>.php</asp:ListItem>                    <asp:ListItem>.aspx</asp:ListItem>                </asp:CheckBoxList>            </p>        </div>

后台页面加载时列出网站所有文件夹列表,跟CheckBoxList绑定,用CheckBoxList选定那个文件夹的内容列出

 protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            url = HttpContext.Current.Request.Url.Host;            weburl.Text = "http://" + HttpContext.Current.Request.Url.Host;            dirpath = Server.MapPath("~/").ToString();            GetAllDirList(dirpath);            CheckBoxList2.Items.Clear();            CheckBoxList2.Items.Add(new ListItem("根文件夹", dirpath));            for (int i = 0; i < al.Count; i++)            {                string c_vale = al[i].ToString();                string c_text = c_vale.Replace(dirpath, "\\");                CheckBoxList2.Items.Add(new ListItem(c_text, c_vale));                CheckBoxList2.Items[i + 1].Selected = true;            }        }    }

实际的会这样列出文件夹。因为在测试时我只有两个文件夹,不觉得什么,然后上传到网上,列出了所有文件夹快哭了,那么多惊讶


以后再改吧。

接着说。


生成文件代码:

protected void Button2_Click(object sender, EventArgs e)    {        string[] typeitem = { };        string[] dirPc = { };        dirpath = Server.MapPath("~/").ToString();        string typelist = GetCheckBoxList(CheckBoxList1);        typeitem = typelist.Split(',');        string dirPctext = GetCheckBoxList(CheckBoxList2);        if (dirPctext != "")            dirPc = dirPctext.Split(',');        StringBuilder sb = new StringBuilder();        sb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");        sb.AppendLine("<!-- generated-on=\"" + DateTime.Now.ToString("yyyy年M月d日 hh:mm") + "\" --> ");        sb.AppendLine("<urlset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\" \n xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");        sb.AppendLine("  <url>");        sb.AppendLine("    <loc>" + weburl.Text + "</loc>");        sb.AppendLine("    <lastmod>" + DateTime.Now.ToString("yyyy-MM-dd'T'hh:mm:ss+00:00") + "</lastmod>");        sb.AppendLine("    <changefreq>monthly</changefreq>");        sb.AppendLine("    <priority>1</priority>");        sb.AppendLine("  </url>");        if (typelist != "" && dirPctext != "")        {            for (int i = 0; i < dirPc.Length; i++)            {                sb.Append(GetAllFiles(typeitem, dirPc[i].ToString()).ToString());            }        }        sb.AppendLine("</urlset>");        Literal1.Text = sb.ToString();    }

存储文件:

 protected void Button3_Click(object sender, EventArgs e)    {        if (Literal1.Text != "")        {            string path = HttpContext.Current.Server.MapPath("~/sitemap.xml");//保存位置            dirpath = Server.MapPath("~/").ToString();            using (StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.UTF8))            {                sw.WriteLine(Literal1.Text);                sw.Flush();                sw.Close();            }            ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('sitemap.xml已生成')", true);        }        else            System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('请生成源文件!先点击\"查看源文件\"')</script>");    }

基本这样,因为时间不多,以后再改善。如果你有更好的方法或建议请留言,谢谢!

生成方案、源码下载

查看演示

0 0