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
- C# 网站sitemap.xml生成工具
- PHP生成网站sitemap.xml
- Django生成网站的sitemap.xml
- .net 生成网站地图 sitemap.xml
- 网站地图Sitemap.xml笔记
- java 定时器生成sitemap.xml
- 如何制作网站地图(sitemap.html和sitemap.xml)?
- 提升网站建设效果之自动生成站点地图sitemap.xml
- 如何让帝国CMS如何自动生成sitemap.xml网站地图文件
- 创建Google网站地图Sitemap.xml
- ecshop生成静态网站地图sitemap.html
- 谈谈google sitemap网站地图工具参数设置
- 网站地图制作工具 Sitemap Creator 2.1
- sitemap.xml生成方法(asp和php)
- Dedecms生成sitemap.xml的简单方法
- sitemap生成工具,需要net框架支持
- Google sitemap 生成工具免费下载
- sitemap.xml
- js实现表格table的升序降序功能
- redis-3.2.5 cluster install
- 用递归函数实现十进制转八进制
- 《Redis系列专题》 之 大规模互联网应用Redis架构要点(精华)
- 我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
- C# 网站sitemap.xml生成工具
- GPU&CUDA
- 初探nginx架构
- MyEclipse按Shift+Ctrl+F格式化代码后调整行的长度
- Word 2007 文本粘贴快捷键及无法输入西文引号等问题
- OpenGL 矩阵变换
- CentOS6.5升级内核到3.10.28
- 自动完成文本框(AutoCompleteTextView)的功能及用法
- 《明解C语言》示例代码和练习代码[第1章]