我直接把我写过的方法复制过来,大家想用的直接改进即可
[C#] 纯文本查看 复制代码 private void button4_Click(object sender, EventArgs e)
{
//87800
//Thread_Main(i + 1);
double stid = Convert.ToDouble(txtstid.Text.Trim());
double etid = Convert.ToDouble(txtetid.Text.Trim());
string sitemap = string.Empty;
// 开启线程
Thread pingTask = new Thread(new ThreadStart(delegate
{
for (double i = stid; i <= etid; i++)
{
try
{
ckb_com objcom = ckbbll.FindListOne(" select es_id from ckb_com where com_id=" + i.ToString());
string urls = $"http://www.chakebao.cn/com/0/{objcom.es_id}";
sitemap += $"<url><loc>{urls}</loc></url>\n";
SetDateDgv(urls);
txtstid.Text = i.ToString();
}
catch (Exception ex)
{
SetDateDgv(ex.Message);
}
}
string sitemapxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><urlset>" + sitemap + "</urlset>";
string mapname = $"sitemap{stid}_{etid}.xml";
using (StreamWriter sw = new StreamWriter($"D:\\sufei\\sitemap\\{mapname}", false, Encoding.UTF8))
{
sw.Write(sitemapxml);
}
richTextBox1.Text = mapname + "成功";
}));
//启动线程
pingTask.Start();
}
生成的格式如下
[C#] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?><urlset>
<url>
<loc>http://www.chakebao.cn/</loc>
</url>
<url>
<loc>http://www.chakebao.cn/222</loc>
</url>
<url>
<loc>http://www.chakebao.cn/1234567</loc>
</url>
</urlset>
|