程序生成SiteMapPath文件

 1 //创建站点地图
 2         private void CreateSiteMap(DataSet ds)
 3         {
 4 
 5             XmlDeclaration declareation;
 6             declareation = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
 7             xmlDoc.AppendChild(declareation);
 8 
 9             XmlElement xeRoot = xmlDoc.CreateElement("siteMap");
10             xmlDoc.AppendChild(xeRoot);
11 
12             XmlElement xroot = xmlDoc.CreateElement("siteMapNode");
13             xroot.SetAttribute("title", "");
14             xroot.SetAttribute("url", "#");
15             xeRoot.AppendChild(xroot);
16 
17             for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
18             {
19                 DataRowView row = ds.Tables[0].DefaultView[i];
20 
21                 string MainMenu = row["MainMenu"].ToString();
22                 string NavigateUrl = row["NavigateUrl"].ToString();
23                 if (MainMenu != str)
24                 {
25                     XmlElement siteMapNode = xmlDoc.CreateElement("siteMapNode");
26                     siteMapNode.SetAttribute("title", MainMenu);
27                     siteMapNode.SetAttribute("description", "");
28                     siteMapNode.SetAttribute("url", NavigateUrl);
29                     xroot.AppendChild(siteMapNode);
30                     str = AddChildNode(MainMenu);
31                 }
32             }
33             xmlDoc.Save(Server.MapPath("\Web.sitemap"));
34         }
35 
36         //添加子节点
37         private string AddChildNode(String text)
38         {
39             string sql = "select * from Menu Where MainMenu ='" + text + "'";
40             DataSql data = new DataSql();
41             data.DataCon();
42             DataSet ds = data.GetDataset(sql);
43             XmlNode root = xmlDoc.SelectSingleNode("/siteMap/siteMapNode/siteMapNode[@title='" + text + "']");
44             for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
45             {
46                 DataRowView row = ds.Tables[0].DefaultView[i];
47 
48                 string ChildMenu = row["ChildMenu"].ToString();
49                 if (ChildMenu != "")
50                 {
51                     string NavigateUrl = row["NavigateUrl"].ToString();
52 
53                     XmlElement siteMapNode = xmlDoc.CreateElement("siteMapNode");
54                     siteMapNode.SetAttribute("title", ChildMenu);
55                     siteMapNode.SetAttribute("description", "");
56                     siteMapNode.SetAttribute("url", NavigateUrl);
57                     root.AppendChild(siteMapNode);
58                 }
59             }
60             return text;
61         }
原文地址:https://www.cnblogs.com/varorbc/p/3424888.html