【NET】生成SiteMap网站地图

之前没有用过sitemap,最近做项目接触到sitemap,做下记录

生成如下XML格式文件

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<urlset>
<url>
<loc>URL地址</loc>
<lastmod>最新修改日期</lastmod>
<changefreq>更新频率</changefreq>
<priority>权重</priority>
</url>
</urlset>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/// <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;
}
}
}
}