本文实例讲述了C#递归读取XML菜单数据的方法。分享给大家供大家参考。具体分析如下:
最近在研究一些关于C#的一些技术,纵观之前的开发项目的经验,做系统时显示系统菜单的功能总是喜欢把数据写在数据库表,然后直接读取加载到菜单树上显示。
现在想把菜单数据都放在XML里,然后递归读取XML。
由于项目使用WCF,实体类使用了两个,一个是业务逻辑层中的实体,一个是调用业务逻辑层递归方法后进行数据实体的转换,XML读取方法写在业务逻辑层中。
思路:
1.先读取XML里所有的菜单
2.根据用户的权限显示所属用户的菜单加载到页面上
XML数据如下:
<?xml version="1.0" encoding="utf-8"?>
<ZCSoft.Net xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Applications>
<Application ID ="OA" Text="OA管理系统">
<Modules>
<Module ID="OA_System" Text="系统管理">
<Menus>
<Menu ID="OA_System_UserManager" Text="人员管理" URL="System/UserManager/UserManagerList.aspx"> </Menu>
<Menu ID="OA_System_RoleManager" Text="角色管理" URL="System/RoleManager/RoleManagerList.aspx"></Menu>
<Menu ID="OA_System_LoginLog" Text="登录日志" URL="System/Log/LoginLogList.aspx"></Menu>
<Menu ID="OA_System_OperateLog" Text="操作日志" URL="System/Log/OperateLogList.aspx"></Menu>
</Menus>
</Module>
<Module ID="OA_TargetManage" Text="目标管理">
<Menus>
<Menu ID="OA_TargetManage_TargetSetup" Text="目标设定" URL="OA/TargetManage/TargetSetupList.aspx">
</Menu>
</Menus>
</Module>
</Modules>
</Application>
</ZCSoft.Net>
</div>
菜单的业务逻辑实体类:
public class MenuTreeSearchModel
{
//菜单ID
public string ItemCode { get; set; }
//菜单名称
public string ItemName { get; set; }
//菜单显示类型
public string ItemType { get; set; }
//排序
public int ItemOrder { get; set; }
//是否显示
public bool Visible { get; set; }
//菜单链接
public string ItemUrl { get; set; }
//上级ID
public string ParentItem { get; set; }
//系统平台ID
public string ApplicationCode { get; set; }
//系统平台名称
public string ApplicationName { get; set; }
//模块ID
public string ModuleCode { get; set; }
//模块名称
public string ModuleName { get; set; }
}
</div>
递归方法,读取每个模块和模块下的菜单:
protected void GetChildMenuList(XElement root, List<MenuTreeSearchModel> menuTreeList)
{
var firstNode = root.FirstNode as XElement;//读取root节点内的第一个节点
if (null != firstNode)
{
//读取root节点下面同级的所有节点
var appList =
from ele in root.Element(firstNode.Name.LocalName).Elements()
select ele;
bool thisVisible = true;//默认节点是可见的
XAttribute thisAttr = root.Attribute("Display");
if (null != thisAttr)//如果菜单的上级模块有显示属性
{
string thisDisplay = thisAttr.Value;
thisVisible = thisDisplay.ToLower() == "false" ? false : true;
}
foreach (var application in appList)
{
//模块Display属性
XAttribute modAttr = application.Attribute("Display");
bool visible = true;
if (null != modAttr)
{
string display = application.Attribute("Display").Value;
visible = display.ToLower() == "false" ? false : true;
}
var nextNode = application.FirstNode as XElement;//该节点的下级节点
string itemType = "Folder";//目录还是菜单
string itemUrl = null;//链接地址
string parentItem = null;//上一节点ID
string applicationCode = null;//平台编码
string applicationName = null;//平台名称
string moduleCode = null;//模块编码
string moduleName = null;//模块名称
if (application.Name.LocalName == "Application")
{
applicationCode = application.Attribute("ID").Value;
applicationName = application.Attribute("Text").Value;
}
if (application.Name.LocalName == "Module")
{
moduleCode = application.Attribute("ID").Value;
moduleName = application.Attribute("Text").Value;
applicationCode = root.Attribute("ID").Value;
applicationName = root.Attribute("Text").Value;
if (thisVisible) //如果该模块的所属平台中的Display属性设置为可见true(注意:没有设置则默认为可见),则模块的上级为Application的ID
{