c#生成站点地图(SiteMapPath)文件示例程序
//创建站点地图
private void createsitemap(dataset ds)
{
xmldeclaration declareation;
declareation = xmldoc.createxmldeclaration("1.0", "utf-8", null);
xmldoc.appendchild(declareation);
xmlelement xeroot = xmldoc.createelement("sitemap");
xmldoc.appendchild(xeroot);
xmlelement xroot = xmldoc.createelement("sitemapnode");
xroot.setattribute("title", "");
xroot.setattribute("url", "#");
xeroot.appendchild(xroot);
for (int i = 0; i <= ds.tables[0].rows.count - 1; i++)
{
datarowview row = ds.tables[0].defaultview[i];
string mainmenu = row["mainmenu"].tostring();
string navigateurl = row["navigateurl"].tostring();
if (mainmenu != str)
{
xmlelement sitemapnode = xmldoc.createelement("sitemapnode");
sitemapnode.setattribute("title", mainmenu);
sitemapnode.setattribute("description", "");
sitemapnode.setattribute("url", navigateurl);
xroot.appendchild(sitemapnode);
str = addchildnode(mainmenu);
}
}
xmldoc.save(server.mappath("\\web.sitemap"));
}
//添加子节点
private string addchildnode(string text)
{
string sql = "select * from menu where mainmenu ='" + text + "'";
datasql data = new datasql();
data.datacon();
dataset ds = data.getdataset(sql);
xmlnode root = xmldoc.selectsinglenode("/sitemap/sitemapnode/sitemapnode[@title='" + text + "']");
for (int i = 0; i <= ds.tables[0].rows.count - 1; i++)
{
datarowview row = ds.tables[0].defaultview[i];
string childmenu = row["childmenu"].tostring();
if (childmenu != "")
{
string navigateurl = row["navigateurl"].tostring();
xmlelement sitemapnode = xmldoc.createelement("sitemapnode");
sitemapnode.setattribute("title", childmenu);
sitemapnode.setattribute("description", "");
sitemapnode.setattribute("url", navigateurl);
root.appendchild(sitemapnode);
}
}
return text;
}