【NET】生成SiteMap网站地图 发表于 2017-02-12 之前没有用过sitemap,最近做项目接触到sitemap,做下记录 生成如下XML格式文件123456789<?xml version="1.0" encoding="utf-8"?><urlset> <url> <loc>URL地址</loc> <lastmod>最新修改日期</lastmod> <changefreq>更新频率</changefreq> <priority>权重</priority> </url></urlset> 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153/// <summary>/// 初始化站点列表/// </summary>/// <param name="list"></param>public static void CreateSiteXml(Dictionary<Guid, string> list){ XmlDocument doc = null; XmlElement root = null; int allCount = list.Count; // 网址不得超过 5 万个,且文件大小不得超过 10 MB const int siteCountFile = 30000; int createMod = allCount / siteCountFile + (allCount % siteCountFile > 0 ? 1 : 0); // 循环创建XML for (int i = 1; i <= createMod; i++) { doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); root = doc.CreateElement("urlset"); int currMod = 0; // 循环添加URL foreach (var item in list.Skip((i - 1) * siteCountFile)) { if (currMod < siteCountFile) { // 链接地址,长度不得超过256字节 XmlElement url = doc.CreateElement("url"); XmlElement url_loc = doc.CreateElement("loc"); url_loc.InnerText = item.Value.ToLower(); url.AppendChild(url_loc); // 链接的最后更新时间 XmlElement url_lastmod = doc.CreateElement("lastmod"); url_lastmod.InnerText = DateTime.Now.ToString("yyyy-MM-dd"); url.AppendChild(url_lastmod); // 链接可能会出现的更新频率 XmlElement url_changefreq = doc.CreateElement("changefreq"); url_changefreq.InnerText = "daily"; url.AppendChild(url_changefreq); // 链接的优先权比值,此值定于0.0-1.0之间 XmlElement url_priority = doc.CreateElement("priority"); url_priority.InnerText = "0.8"; url.AppendChild(url_priority); root.AppendChild(url); } currMod++; } doc.AppendChild(root); // 保存XML string SiteMapDir = System.Web.HttpContext.Current.Server.MapPath("/SiteMap/sitemap"); string fullFileName = SiteMapDir + string.Format("{0:d5}", i) + ".xml"; doc.Save(fullFileName); }}/// <summary>/// 插入/// </summary>/// <param name="str"></param>public static void InsertSiteXml(string str){ if (string.IsNullOrWhiteSpace(str)) return; const int siteCountFile = 30000; string SiteMapDir = System.Web.HttpContext.Current.Server.MapPath("/SiteMap/"); string[] file = Directory.GetFiles(SiteMapDir); for (int i = 1; i < file.Length; i++) { XmlDocument xml = new XmlDocument(); try { xml.Load(file[i]); } catch (Exception ex) { throw new Exception(ex.Message); } XmlNodeList xmlNodes = xml.SelectNodes("/urlset/url"); if (xmlNodes.Count < siteCountFile) { XmlNode _root = xml.SelectSingleNode("/urlset"); XmlNode _url = xml.CreateElement("url"); XmlNode _url_loc = xml.CreateElement("loc"); _url_loc.InnerText = str.Trim().ToLower(); _url.AppendChild(_url_loc); _root.AppendChild(_url); xml.Save(file[i]); return; } } XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(dec); XmlElement root = doc.CreateElement("urlset"); XmlElement url = doc.CreateElement("url"); XmlElement url_loc = doc.CreateElement("loc"); url_loc.InnerText = str.Trim().ToLower(); url.AppendChild(url_loc); root.AppendChild(url); doc.AppendChild(root); // 保存XML string fullFileName = SiteMapDir + string.Format("sitemap{0:d5}", file.Count()) + ".xml"; doc.Save(fullFileName);}/// <summary>/// 删除/// </summary>/// <param name="str"></param>public static void DeleteSiteXml(string str){ if (string.IsNullOrWhiteSpace(str)) return; string SiteMapDir = System.Web.HttpContext.Current.Server.MapPath("/SiteMap/"); string[] file = Directory.GetFiles(SiteMapDir); for (int i = 0; i < file.Length; i++) { XmlDocument xml = new XmlDocument(); try { xml.Load(file[i]); } catch (Exception ex) { throw new Exception(ex.Message); } XmlNodeList xmlNodes = xml.SelectNodes("/urlset/url"); for (int k = 0; k < xmlNodes.Count; k++) { XmlNode node = xmlNodes[k]; string text = node.FirstChild.InnerText; if (text.Equals(str.Trim(), StringComparison.OrdinalIgnoreCase)) { XmlNode _root = xml.SelectSingleNode("/urlset"); _root.RemoveChild(node); xml.Save(file[i]); return; } } }}