在博客园注册了有4年了,很遗憾至今仍未发表过博客,趁周末有空发表第一篇博客。小生不才,在此献丑了!
最近在研究一些关于C#的一些技术,纵观之前的开发项目的经验,做系统时显示系统菜单的功能总是喜欢把数据写在数据库表,然后直接读取加载到菜单树上显示。
现在想把菜单数据都放在XML里,然后递归读取XML。
由于项目使用WCF,实体类使用了两个,一个是业务逻辑层中的实体,一个是调用业务逻辑层递归方法后进行数据实体的转换,XML读取方法写在业务逻辑层中。
思路:1.先读取XML里所有的菜单2.根据用户的权限显示所属用户的菜单加载到页面上
XML数据如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <ZCSoft.Net xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 3 <applications> 4 <Application ID ="OA" Text="OA管理系统"> 5 <Modules> 6 <Module ID="OA_System" Text="系统管理"> 7 <Menus> 8 <Menu ID="OA_System_UserManager" Text="人员管理" URL="System/UserManager/UserManagerList.aspx"> </Menu> 9 <Menu ID="OA_System_RoleManager" Text="角色管理" URL="System/RoleManager/RoleManagerList.aspx"></Menu>10 <Menu ID="OA_System_LoginLog" Text="登录日志" URL="System/Log/LoginLogList.aspx"></Menu>11 <Menu ID="OA_System_OperateLog" Text="操作日志" URL="System/Log/OperateLogList.aspx"></Menu>12 </Menus>13 </Module>14 15 <Module ID="OA_TargetManage" Text="目标管理">16 <Menus>17 <Menu ID="OA_TargetManage_TargetSetup" Text="目标设定" URL="OA/TargetManage/TargetSetupList.aspx">18 </Menu>19 </Menus>20 </Module>21 </Modules>22 </Application>23 </ZCSoft.Net>View Code
菜单的业务逻辑实体类:
1 public class MenuTreeSearchModel 2 { 3 //菜单ID 4 public string ItemCode { get; set; } 5 6 //菜单名称 7 public string ItemName { get; set; } 8 9 //菜单显示类型10 public string ItemType { get; set; }11 12 //排序13 public int ItemOrder { get; set; }14 15 //是否显示16 public bool Visible { get; set; }17 18 //菜单链接19 public string ItemUrl { get; set; }20 21 //上级ID22 public string ParentItem { get; set; }23 24 //系统平台ID25 public string ApplicationCode { get; set; }26 27 //系统平台名称28 public string ApplicationName { get; set; }29 30 //模块ID31 public string ModuleCode { get; set; }32 33 //模块名称34 public string ModuleName { get; set; }35 }View Code
递归方法,读取每个模块和模块下的菜单:
1 PRotected void GetChildMenuList(XElement root, List<MenuTreeSearchModel> menuTreeList) 2 { 3 var firstNode = root.FirstNode as XElement;//读取root节点内的第一个节点 4 if (null != firstNode) 5 { 6 //读取root节点下面同级的所有节点 7 var appList = 8 from ele in root.Element(firstNode.Name.LocalName).Elements() 9 select ele;10 11 bool thisVisible = true;//默认节点是可见的12 XAttribute thisAttr = root.Attribute("Display");13 if (null != thisAttr)//如果菜单的上级模块有显示属性14 {15 string thisDisplay = thisAttr.Value;16 thisVisible = thisDisplay.ToLower() == "false" ? false : true;17 }18 19 foreach (var application in appList)20 {21 //模块Display属性22 XAttribute modAttr = application.Attribute("Display");23 bool visible = true;24 if (null != modAttr)25 {26 string display = application.Attribute("Display").Value;27 visible = display.ToLower() == "false" ? false : true;28 }29 var nextNode = application.FirstNode as XElement;//该节点的下级节点30 31 string itemType = "Folder";//目录还是菜单32 string itemUrl = null;//链接地址33 string parentItem = null;//上一节点ID34 string applicationCode = null;//平台编码35 string applicationName = null;//平台名称36 string moduleCode = null;//模块编码37 string moduleName = null;//模块名称38 39 if (application.Name.LocalName == "Application")40 {41 applicationCode = application.Attribute("ID").Value;42 applicationName = application.Attribute("Text").Value;43 }44 45 if (application.Name.LocalName == "Module")46 {47 moduleCode = application.Attribute("ID").Value;48 moduleName = application.Attribute("Text").Value;49 applicationCode = root.Attribute("ID").Value;50 applicationName = root.Attribute("Text").Value;51 52 if (thisVisible) //如果该模块的所属平台中的Display属性设置为可见true(注意:没有设置则默认为可见),则模块的上级为Application的ID53 {54 parentItem = root.Attribute("ID").Value;55 }56 }57 58 if (application.Name.LocalName == "Menu")59 {60 itemType = "Menu";61 itemUrl = application.Attribute("URL").Value;62 moduleCode = root.Attribute("ID").Value;63 moduleName = root.Attribute("Text").Value;64 applicationCode = root.Parent.Parent.Attribute("ID").Value;65 applicationName = root.Parent.Parent.Attribute("Text").Value;66 67 if (thisVisible) //如果该菜单的所属模块中的Display属性设置为可见true(注意:没有设置则默认为可见),则菜单的上级为Module的ID68 {69 parentItem = root.Attribute("ID").Value;70 }71 else//如果该菜单的所属模块中的Display属性设置为不可见false,则菜单的上级为Application的ID72 {73 parentItem = root.Parent.Parent.Attribute("ID").Value;74 }75 }76 77 MenuTreeSearchModel model = new MenuTreeSearchModel();78 model.ItemCode = application.Attribute("ID").Value;79 model.ItemName = application.Attribute("Text").Value;80 model.ItemType = itemType;81 model.ItemOrder = 0;82 model.Visible = visible;83 model.ItemUrl = itemUrl;84 model.ParentItem = parentItem;85 model.ApplicationCode = applicationCode;86 model.ApplicationName = applicationName;87 model.ModuleCode = moduleCod
新闻热点
疑难解答