asp.net core3.1 c# 生成三种sitemap,今天发个汇总的。代码还未重构,哪位大咖自己重构一下吧。先看看成果物截图:
统一放到网站根目录就行。 asp.net core3.1的根目录是wwwroot下,接下来我会升级项目到NET6.
运行的成果物:
网站地图三种格式生成代码:
#region CreateSiteMap
/// <summary>
///
/// </summary>
/// <returns></returns>
[Route("/system/ajaxCreateSiteMap")]
public JsonResult CreateSiteMap()
{
try
{
//XML
string currentXmlFileName = "sitemap.xml";
string sitemapPath = _Environment.WebRootPath;
string sitemapFileFullNamePath = string.Format(sitemapPath + "/{0}", currentXmlFileName);
//TXT
string sitemaptxtname = "sitemap.txt";
string sitemapTxTFileFullNamePath = string.Format(sitemapPath + "/{0}", sitemaptxtname);
FileStream fs = new FileStream(sitemapTxTFileFullNamePath, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("https://www.yadinghao.com");
//html
string sitemaphtmlname = "sitemap.html";
string sitemapHtmlFileFullNamePath = string.Format(sitemapPath + "/{0}", sitemaphtmlname);
FileStream htmlfs = new FileStream(sitemapHtmlFileFullNamePath, FileMode.Create, FileAccess.Write);
StreamWriter htmlsw = new StreamWriter(htmlfs, System.Text.Encoding.UTF8);
htmlsw.BaseStream.Seek(0, SeekOrigin.End);
StringBuilder sb= new StringBuilder();
sb.AppendLine("<!DOCTYPE html>");
sb.AppendLine("<html lang='en'>");
sb.AppendLine("<head>");
sb.AppendLine("<meta charset='UTF-8'>");
sb.AppendLine("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
sb.AppendLine("<title>sitemap</title>");
sb.AppendLine("</head>");
sb.AppendLine("<body>");
sb.AppendLine("<style>a{color:rgb(0, 0, 238);font-size:13px;}</style>");
sb.AppendLine("<ol><li><a href='https://www.yadinghao.com'>亚丁号</a></li>");
var articleList = new ArticleAccess().FindArticleListForSitemap(); //获取需要生成sitemmap的数据
XmlDocument xmlDoc = new XmlDocument();
//加入XML的声明段落:<?xmlversion="1.0" encoding="utf-8"?>
XmlDeclaration xmldecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDoc.AppendChild(xmldecl);
XmlElement xe = xmlDoc.CreateElement("urlset");//创建一个urlset根元素
xmlDoc.AppendChild(xe);
XmlNode root = xmlDoc.SelectSingleNode("urlset");//查找<urlset>
XmlElement xmle = xmlDoc.CreateElement("url"); //创建一个<api>节点
XmlElement xmle1 = xmlDoc.CreateElement("loc");
xmle1.InnerText = "https://www.yadinghao.com";//此处拼接动态地址
xmle.AppendChild(xmle1);
XmlElement xmle2 = xmlDoc.CreateElement("lastmod");
xmle2.InnerText = Tools.GetDate();
xmle.AppendChild(xmle2);
XmlElement xmle3 = xmlDoc.CreateElement("changefreq");
xmle3.InnerText = "always";
xmle.AppendChild(xmle3);
XmlElement xmle4 = xmlDoc.CreateElement("priority");
xmle4.InnerText = "1";
xmle.AppendChild(xmle4);
xe.AppendChild(xmle);
foreach (var item in articleList)
{
var spriderUrl = string.Format("https://www.yadinghao.com/article/details/{0}.html", item.ARTICLE_CODE);
sw.WriteLine(spriderUrl);
sb.AppendLine("<li><a href='"+ spriderUrl + "'>"+ item.ARTICLE_TITLE+ "</a></li>");
XmlElement xe1 = xmlDoc.CreateElement("url"); //创建一个<api>节点
XmlElement xesub1 = xmlDoc.CreateElement("loc");
xesub1.InnerText = spriderUrl;//此处拼接动态地址
xe1.AppendChild(xesub1);
XmlElement xesub2 = xmlDoc.CreateElement("lastmod");
xesub2.InnerText = item.CREATEDATE.ToString("yyyy-MM-dd");
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmlDoc.CreateElement("changefreq"); //是用来告诉搜索引擎网站更新的周期,描述的单词:“always”(经常) 、“hourly”(每时)、“daily”(每天)、“weekly”(每周)、“monthly”(每月)、“yearly”(每年)。像首页就可以用“always”;对于很久前的链接或不再更新内容的链接就可以使用“yearly”。
xesub3.InnerText = "yearly";
xe1.AppendChild(xesub3);
XmlElement xesub4 = xmlDoc.CreateElement("priority");
xesub4.InnerText = "0.8";
xe1.AppendChild(xesub4);
root.AppendChild(xe1);
}
xmlDoc.Save(sitemapFileFullNamePath); //此处放xml文件的保存地址
sb.AppendLine("</ol>");
sb.AppendLine("</body>");
sb.AppendLine("</html>");
htmlsw.WriteLine(sb.ToString());
htmlsw.Flush();
htmlsw.Close();
sw.Flush();
sw.Close();
return JsonResultRight();
}
catch (Exception ex)
{
_Logger.LogError("ajaxCreateSiteMapXml生成数据失败:" + ex.Message);
return JsonResultError("ajaxCreateSiteMapXml生成数据失败:" + ex.Message);
}
}
#endregion
页面加载各种地图格式:
public static int readFileLines(string path) //这里的参数是txt所在路径
{
int lines = 0; //用来统计txt行数
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs);
while (sr.ReadLine() != null)
{
lines++;
}
fs.Close();
sr.Close();
return lines;
}
/// <summary>
///
/// </summary>
/// <param name="path"></param>
/// <param name="tag"></param>
/// <returns></returns>
public static int readHtmlTagLines(string path,string tag)
{
int lines = 0; //用来统计tag行数
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs);
StringBuilder sb = new StringBuilder();
string ss = "";
ss = sr.ReadToEnd();
string[] strings= ss.Split(tag);
lines = strings.Length;
//while (sr.ReadLine() != null)
//{
// string lineMessage = sr.ReadLine();
// if (lineMessage == null)
// {
// continue;
// }
// else
// {
// sb.Append(lineMessage);
// }
// //if (lineMessage.Contains(tag))
// //{
// // lines++;
// //}
//}
//string html= sb.ToString();
////IHtmlDocument source = new JumonyParser().LoadDocument(path, System.Text.Encoding.GetEncoding("utf-8"));
////string html = wc.DownloadString("https://www.baidu.com/");
////以正则表达式的形式匹配到字符串网页中想要的数据
//MatchCollection matches = Regex.Matches(html, "<a.*>(.*)</a>");
////依次取得匹配到的数据
//foreach (Match item in matches)
//{
// string hhhhh = item.Groups[1].Value;
// Console.WriteLine(hhhhh);
//}
fs.Close();
sr.Close();
return lines;
}
[Route("/system/ajaxLoadSiteMaps")]
public JsonResult LoadSiteMaps()
{
try
{
List<SiteMapEntity> resultList = new List<SiteMapEntity>();
string filePath = _Environment.WebRootPath;
//sitemap
DirectoryInfo root = new DirectoryInfo(filePath);
FileInfo[] files = root.GetFiles();
int MB = 1024 * 1024;
foreach (FileInfo file in files)
{
string extension = Path.GetExtension(file.FullName);//扩展名
if (file.Name.Contains("sitemap") & extension == ".html")
{
string fileSize = Math.Round(file.Length / (float)MB, 2).ToString();
SiteMapEntity model = new SiteMapEntity();
model.SITEMAP_ID = Tools.GetGUID();
model.SITEMAP_MD5 = Tools.MD5String(file.FullName);
model.SITEMAP_NAME = file.Name;
model.SITEMAP_SIZE = fileSize + "MB";
int fileTxTNumber = readHtmlTagLines(file.FullName, "href");
model.SITEMAP_NUMBER = fileTxTNumber.ToString();
model.CREATEDATE = file.LastWriteTime;
using (StreamReader sr = new StreamReader(file.FullName, FileEncoding.GetType(file.FullName)))
{
string sTemp = string.Empty;
string sCurEncode = sr.CurrentEncoding.EncodingName;
model.SITEMAP_ENCODING = sCurEncode;
}
resultList.Add(model);
}
if (file.Name.Contains("sitemap") & extension == ".txt")
{
string fileSize = Math.Round(file.Length / (float)MB, 2).ToString();
SiteMapEntity model = new SiteMapEntity();
model.SITEMAP_ID = Tools.GetGUID();
model.SITEMAP_MD5 = Tools.MD5String(file.FullName);
model.SITEMAP_NAME = file.Name;
model.SITEMAP_SIZE = fileSize + "MB";
int fileTxTNumber = readFileLines(file.FullName);
model.SITEMAP_NUMBER = fileTxTNumber.ToString();
model.CREATEDATE = file.LastWriteTime;
using (StreamReader sr = new StreamReader(file.FullName, FileEncoding.GetType(file.FullName)))
{
string sTemp = string.Empty;
string sCurEncode = sr.CurrentEncoding.EncodingName;
model.SITEMAP_ENCODING = sCurEncode;
}
resultList.Add(model);
}
if (file.Name.Contains("sitemap") & extension == ".xml")
{
string fileSize = Math.Round(file.Length / (float)MB, 2).ToString();
SiteMapEntity model = new SiteMapEntity();
model.SITEMAP_ID = Tools.GetGUID();
model.SITEMAP_MD5 = Tools.MD5String(file.FullName);
model.SITEMAP_NAME = file.Name;
model.SITEMAP_SIZE = fileSize + "MB";
using (StreamReader sr = new StreamReader(file.FullName, FileEncoding.GetType(file.FullName)))
{
string sTemp = string.Empty;
string sCurEncode = sr.CurrentEncoding.EncodingName;
model.SITEMAP_ENCODING = sCurEncode;
}
int nodeCount = 0;
XmlDocument doc = new XmlDocument();
doc.Load(file.FullName);
//获得根节点
XmlNode rootNode = doc.DocumentElement;
//在根节点中寻找节点
foreach (XmlNode node in rootNode.ChildNodes)
{
//找到对应的节点
if (node.Name == "url")
{
nodeCount = nodeCount + 1;
}
}
model.SITEMAP_NUMBER = nodeCount.ToString();
model.CREATEDATE = file.LastWriteTime;
resultList.Add(model);
}
}
return ListToJsonString(resultList.Count, resultList);
}
catch (Exception ex)
{
_Logger.LogError("ajaxLoadSiteMaps加载数据失败:" + ex.Message);
return JsonResultError("加载数据失败" + ex.Message);
}
}
#endregion
加载的时候已经考虑地图文件分割的问题.