在网上找了一圈没有找到通过数据库生成web.sitemap文件的代码,只好自己写了
为了能够动态加载sitemap到devexpress控件的sitemapcontrol件上,写了这个将数据库表转换为sitemap的类库,需要的兄弟只需要将
其中调用数据库的方法换成自己的就可以很简单的生成相应的sitemap文件(不是自己写的多好,只不过这东西重复写太没意思),其它的就不多说了,用上了一点LINQ TO XML的东西,感觉LINQ还是很好用的,呵呵
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class SqlSiteMapGen
{
private XDocument siteMap;
private const string SiteMapSchemas = "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0";
private const string SiteMap_Name = "siteMap";
private const string SiteMapNode_Name = "siteMapNode";
private const string SiteMapNode_Attribute_Url = "url";
private const string SiteMapNode_Attribute_Title = "title";
private const string SiteMapNode_Attribute_Description = "description";
public XDocument CreateSiteMapNode()
{
siteMap = new XDocument(new XDeclaration("1.0", "utf-8",""));
XNamespace xw = SiteMapSchemas;
XElement rootNode = new XElement(SiteMap_Name);
XElement mapNode = CreateNode("后台管理菜单", "", "");
rootNode.Add(CreateSiteMapNode(mapNode, "00000000-0000-0000-0000-000000000000"));
siteMap.Add(rootNode);
return siteMap;
}
private static XElement CreateSiteMapNode(XElement node,string PrentID)
{
XElement childnode;
Debug.Print(PrentID);
using (Mder_CenterEntities ctx = new Mder_CenterEntities())
{
var query = from c in ctx.COMM_HUMAN_FUNCTION
where c.LOCKED_IF == "0" && c.FUNCTION_PID == PrentID
orderby c.SORT
select new { c.FUNCTION_ID, c.FUNCTION_NAME, c.FUNCTION_PID, c.URL };
foreach (var item in query)
{
childnode = CreateNode(item.FUNCTION_NAME, item.URL, item.FUNCTION_NAME);
CreateSiteMapNode(childnode, item.FUNCTION_ID);
node.Add(childnode);
}
}
return node;
}
private static XElement CreateNode(string sTitel, string sUrl, string sDescription)
{
XElement node = new XElement(
SiteMapNode_Name,
new XAttribute(SiteMapNode_Attribute_Title, sTitel),
new XAttribute(SiteMapNode_Attribute_Url, sUrl??""),
new XAttribute(SiteMapNode_Attribute_Description, sDescription??"")
);
return node;
}
{
private XDocument siteMap;
private const string SiteMapSchemas = "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0";
private const string SiteMap_Name = "siteMap";
private const string SiteMapNode_Name = "siteMapNode";
private const string SiteMapNode_Attribute_Url = "url";
private const string SiteMapNode_Attribute_Title = "title";
private const string SiteMapNode_Attribute_Description = "description";
public XDocument CreateSiteMapNode()
{
siteMap = new XDocument(new XDeclaration("1.0", "utf-8",""));
XNamespace xw = SiteMapSchemas;
XElement rootNode = new XElement(SiteMap_Name);
XElement mapNode = CreateNode("后台管理菜单", "", "");
rootNode.Add(CreateSiteMapNode(mapNode, "00000000-0000-0000-0000-000000000000"));
siteMap.Add(rootNode);
return siteMap;
}
private static XElement CreateSiteMapNode(XElement node,string PrentID)
{
XElement childnode;
Debug.Print(PrentID);
using (Mder_CenterEntities ctx = new Mder_CenterEntities())
{
var query = from c in ctx.COMM_HUMAN_FUNCTION
where c.LOCKED_IF == "0" && c.FUNCTION_PID == PrentID
orderby c.SORT
select new { c.FUNCTION_ID, c.FUNCTION_NAME, c.FUNCTION_PID, c.URL };
foreach (var item in query)
{
childnode = CreateNode(item.FUNCTION_NAME, item.URL, item.FUNCTION_NAME);
CreateSiteMapNode(childnode, item.FUNCTION_ID);
node.Add(childnode);
}
}
return node;
}
private static XElement CreateNode(string sTitel, string sUrl, string sDescription)
{
XElement node = new XElement(
SiteMapNode_Name,
new XAttribute(SiteMapNode_Attribute_Title, sTitel),
new XAttribute(SiteMapNode_Attribute_Url, sUrl??""),
new XAttribute(SiteMapNode_Attribute_Description, sDescription??"")
);
return node;
}