本文实例讲述了C#递归读取XML菜单数据的方法。分享给大家供大家参考。具体分析如下:
最近在研究一些关于C#的一些技术,纵观之前的开发项目的经验,做系统时显示系统菜单的功能总是喜欢把数据写在数据库表,然后直接读取加载到菜单树上显示。
现在想把菜单数据都放在XML里,然后递归读取XML。
由于项目使用WCF,实体类使用了两个,一个是业务逻辑层中的实体,一个是调用业务逻辑层递归方法后进行数据实体的转换,XML读取方法写在业务逻辑层中。
思路:
1.先读取XML里所有的菜单
2.根据用户的权限显示所属用户的菜单加载到页面上
XML数据如下:
菜单的业务逻辑实体类:
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; }
}
递归方法,读取每个模块和模块下的菜单:
protected void GetChildMenuList(XElement root, List
{
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
{
parentItem = root.Attribute("ID").Value;
}
}
if (application.Name.LocalName == "Menu")
{
itemType = "Menu";
itemUrl = application.Attribute("URL").Value;
moduleCode = root.Attribute("ID").Value;
moduleName = root.Attribute("Text").Value;
applicationCode = root.Parent.Parent.Attribute("ID").Value;
applicationName = root.Parent.Parent.Attribute("Text").Value;
if (thisVisible) //如果该菜单的所属模块中的Display属性设置为可见true(注意:没有设置则默认为可见),则菜单的上级为Module的ID
{
parentItem = root.Attribute("ID").Value;
}
else//如果该菜单的所属模块中的Display属性设置为不可见false,则菜单的上级为Application的ID
{
parentItem = root.Parent.Parent.Attribute("ID").Value;
}
}
MenuTreeSearchModel model = new MenuTreeSearchModel();
model.ItemCode = application.Attribute("ID").Value;
model.ItemName = application.Attribute("Text").Value;
model.ItemType = itemType;
model.ItemOrder = 0;
model.Visible = visible;
model.ItemUrl = itemUrl;
model.ParentItem = parentItem;
model.ApplicationCode = applicationCode;
model.ApplicationName = applicationName;
model.ModuleCode = moduleCode;
model.ModuleName = moduleName;
menuTreeList.Add(model);
if (null != nextNode)//如果还有下级节点
{
//调用递归
GetChildMenuList(application, menuTreeList);
}
}
}
}
从XML文档读取:
///
/// 从XML文件读取菜单节点
///
///
public List
{
List
//读取XML文档路径,这里我把XML放在网站的bin目录里
string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "Foundation.xml";
XElement root = XElement.Load(xmlPath);
var appList =
from ele in root.Element("Applications").Elements()
select ele;
//按系统平台筛选
foreach (var application in appList)
{
MenuTreeSearchModel model = new MenuTreeSearchModel();
model.ItemCode = application.Attribute("ID").Value;
model.ItemName = application.Attribute("Text").Value;
model.ItemType = "Folder";
model.ItemOrder = 0;
model.Visible = true;
model.ItemUrl = null;
model.ParentItem = null;
model.ApplicationCode = application.Attribute("ID").Value;
model.ApplicationName = application.Attribute("Text").Value;
model.ModuleCode = null;
model.ModuleName = null;
list.Add(model);
//递归调用
GetChildMenuList(application, list);
}
return list;
}
以下是在调用服务契约方法时进行的实体类:
public class PublicUserMenuTreeData
{
//菜单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; }
//当前菜单下的菜单集合
public List
}
实体转换方法:
public PublicUserMenuTreeData TransferUserMenuTreeToPublicUserMenu(MenuTreeData userMenuTreeData)
{
PublicUserMenuTreeData pubUserMenuTreeData = new PublicUserMenuTreeData();
pubUserMenuTreeData.ItemCode = userMenuTreeData.ItemCode;
pubUserMenuTreeData.ItemName = userMenuTreeData.ItemName;
pubUserMenuTreeData.ItemType = userMenuTreeData.ItemType;
pubUserMenuTreeData.ItemOrder = userMenuTreeData.ItemOrder;
pubUserMenuTreeData.Visible = userMenuTreeData.Visible;
pubUserMenuTreeData.ItemUrl = userMenuTreeData.ItemUrl;
pubUserMenuTreeData.ParentItem = userMenuTreeData.ParentItem;
pubUserMenuTreeData.ApplicationCode = userMenuTreeData.ApplicationCode;
pubUserMenuTreeData.ApplicationName = userMenuTreeData.ApplicationName;
pubUserMenuTreeData.ModuleCode = userMenuTreeData.ModuleCode;
pubUserMenuTreeData.ModuleName = userMenuTreeData.ModuleName;
return pubUserMenuTreeData;
}
用户权限菜单方法:
///
/// 有用户权限树获取共用的用户菜单列表
///
///
///
public List
{
List
List
foreach (var userMenuTreeData in list)
{
PublicUserMenuTreeData pubUserMenuTreeData = TransferUserMenuTreeToPublicUserMenu(userMenuTreeData);
pubUserMenuTreeData.UserMenuTreeDatas = GetChildData(pubUserMenuTreeData.ItemCode, listAllUserMenu);
listPublicUserMenuTreeData.Add(pubUserMenuTreeData);
}
return listPublicUserMenuTreeData;
}
public List
{
List
if (list.Count > 0)
{
List
foreach (var userMenuTreeData in list)
{
PublicUserMenuTreeData pubUserMenuTreeData = TransferUserMenuTreeToPublicUserMenu(userMenuTreeData);
pubUserMenuTreeData.UserMenuTreeDatas = GetChildData(pubUserMenuTreeData.ItemCode, listUserMenuTreeData);
listPublicUserMenuTreeData.Add(pubUserMenuTreeData);
}
return listPublicUserMenuTreeData;
}
return null;
}
系统菜单类:
///
/// 系统菜单
///
[DataContract()]
public class MenuTreeData
{
[DataMember()]
public string ItemCode { get; set; }
[DataMember()]
public string ItemName { get; set; }
[DataMember()]
public string ItemType { get; set; }
[DataMember()]
public int ItemOrder { get; set; }
[DataMember()]
public bool Visible { get; set; }
[DataMember()]
public string ItemUrl { get; set; }
[DataMember()]
public string ParentItem { get; set; }
[DataMember()]
public string ApplicationCode { get; set; }
[DataMember()]
public string ApplicationName { get; set; }
[DataMember()]
public string ModuleCode { get; set; }
[DataMember()]
public string ModuleName { get; set; }
}
后台页面加载Load代码:
string menuData = string.Empty;
var treeList= GetMenuTreeList();
if (treeList!= null)
{
List
List
menuData = JsonConvert.SerializeObject(listPublicUserMenuTreeData);
}
页面加载脚本,这里使用Jquery:
var obj = menuData;
GetMenuInfo(obj);
function GetMenuInfo(obj) {
var str = "";
var objInfo = "";
if (obj) {
objInfo = obj.split("|");
if (objInfo[0] != "") {
var PublicUserMenuTreeData = JSON.parse(objInfo[0]);
for (var i = 0; i < PublicUserMenuTreeData.length; i++) {
str += ("
var tempmenu= PublicUserMenuTreeData[i];
if (tempmenu.ItemType && tempmenu.ItemType == "Menu") {
str += ("" + tempmenu.ItemName + "");
str += ("