XML 反序列化成对象,绑定到CheckBoxList控件
1、前台
<div class="control-group"> <label class="control-label"> 导航名称:</label> <div class="controls"> <asp:CheckBoxList ID="CboxList" runat="server"> </asp:CheckBoxList> <span class="help-inline"></span> </div> </div>
2、XML文件 MenuConfig.xml
<?xml version="1.0" encoding="utf-8" ?> <MenuConfig> <MenuGroups> <MenuGroup name="管理首页" url="main.aspx" icon="icon-home" info="欢迎登录OAM管理系统!"> </MenuGroup> <MenuGroup name="系统管理" icon="icon-cog"> <MenuArray> <Menu name="用户管理" url="/Account/User/Index" info="添加修改删除用户" permission="Manage_User" /> <Menu name="权限管理" url="/Account/Role/Index" info="添加修改删除角色" permission="Manage_Role" /> <Menu name="日志管理" url="/Account/Role/Index" info="添加修改删除角色" permission="Manage_Log" /> <Menu name="修改密码" url="/Account/Role/Index" info="添加修改删除角色" permission="Modify_Pwd" /> </MenuArray> </MenuGroup> <MenuGroup name="企业信息管理" icon="icon-leaf"> <MenuArray> <Menu name="企业信息列表" url="/Cms/Article/Index" info="添加修改删除文章" permission="Manage_Enterp" /> <Menu name="新增企业(我司)" url="/Cms/Channel/Index" info="添加修改删除文章频道" permission="AddMy_Enterp" /> <Menu name="注册企业(网络)" url="/Cms/Channel/Index" info="添加修改删除文章频道" permission="AddInter_Enterp" /> </MenuArray> </MenuGroup> <MenuGroup name="企业服务管理" icon="icon-sitemap"> <MenuArray> <Menu name="客户反馈" url="/OA/Staff/Index" info="添加修改删除员工信息" permission="Feedback_Manage" /> <Menu name="客户投诉" url="/OA/Branch/Index" info="添加修改删除部门信息" permission="Complaints_Manage" /> </MenuArray> </MenuGroup> <MenuGroup name="企业信息维护管理" icon="icon-phone-sign"> <MenuArray> <Menu name="来访来电" url="/Crm/VisitRecord/Index" info="添加修改删除来访来电" permission="CrmManage_VisitRecord" /> <Menu name="客户管理" url="/Crm/Customer/Index" info="添加修改删除客户" permission="CrmManage_Customer" /> <Menu name="项目管理" url="/Crm/Project/Index" info="添加修改删除项目" permission="CrmManage_Project" /> <Menu name="业绩统计" url="/Crm/Analysis/Index" info="查看业绩统计" permission="CrmManage_Analysis" /> <Menu name="来电统计" url="/Crm/Analysis/VisitStatistics" info="查看来电统计" permission="CrmManage_Analysis" /> </MenuArray> </MenuGroup> <MenuGroup name="消息中心" icon="icon-sitemap"> <MenuArray> <Menu name="客户反馈" url="/OA/Staff/Index" info="添加修改删除员工信息" permission="OAManage_Staff" /> <Menu name="客户投资" url="/OA/Branch/Index" info="添加修改删除部门信息" permission="OAManage_Branch" /> </MenuArray> </MenuGroup> </MenuGroups> </MenuConfig>
3、Model层 MenuConfig.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; namespace OAM.Model { [Serializable] public class MenuConfig { public MenuConfig() { } public MenuGroup[] MenuGroups { get; set; } } [Serializable] [XmlRoot("MenuGroups")] public class MenuGroup { [XmlArray("MenuArray")] [XmlArrayItem("Menu")] public List<Menu> MenuArray { get; set; } [XmlAttribute("id")] public string Id { get; set; } [XmlAttribute("name")] public string Name { get; set; } [XmlAttribute("url")] public string Url { get; set; } [XmlAttribute("icon")] public string Icon { get; set; } [XmlAttribute("permission")] public string Permission { get; set; } [XmlAttribute("info")] public string Info { get; set; } } [Serializable] [XmlRoot("Menu")] public class Menu { [XmlAttribute("id")] public string Id { get; set; } [XmlAttribute("name")] public string Name { get; set; } [XmlAttribute("url")] public string Url { get; set; } [XmlAttribute("info")] public string Info { get; set; } [XmlAttribute("permission")] public string Permission { get; set; } } }
4、将xml文件内容反序列化成对象
#region XML导航反序列化 绑定CheckBoxList //将xml反序列化为MenuConfig数组对象 private void XmlMenuToModel() { OAM.Model.MenuConfig MenuListArr = XmlDeseriFromfile<OAM.Model.MenuConfig>(Request.MapPath("Config/MenuConfig.xml"), Encoding.UTF8); if (MenuListArr.MenuGroups.Length > 0) { for (int i = 0; i < MenuListArr.MenuGroups.Length; i++) { if (MenuListArr.MenuGroups[i].MenuArray.Count > 0) { for (int j = 0; j < MenuListArr.MenuGroups[i].MenuArray.Count; j++) { ListItem item = new ListItem(MenuListArr.MenuGroups[i].MenuArray[j].Name, MenuListArr.MenuGroups[i].MenuArray[j].Permission); CboxList.Items.Add(item); } } } } } /// <summary> /// 从XML字符串中反序列化对象 /// </summary> /// <typeparam name="T">结果对象类型</typeparam> /// <param name="s">包含对象的XML字符串</param> /// <param name="encoding">编码方式</param> /// <returns>反序列化得到的对象</returns> public T Deser<T>(string xmlstr, Encoding encoding) { XmlSerializer myser = new XmlSerializer(typeof(T)); MemoryStream ms = new MemoryStream(encoding.GetBytes(xmlstr)); StreamReader sr = new StreamReader(ms, encoding); return (T)myser.Deserialize(sr); } /// <summary> /// 读入一个文件,并按XML的方式反序列化对象。 /// </summary> /// <typeparam name="T">结果对象类型</typeparam> /// <param name="path">文件路径</param> /// <param name="encoding">编码方式</param> /// <returns>反序列化得到的对象</returns> public T XmlDeseriFromfile<T>(string path, Encoding encoding) { string xml = File.ReadAllText(path, encoding); return Deser<T>(xml, encoding); } #endregion
搞了3小时才弄出这么点来代码,大家多支持一下。有什么问题留言,请教大神留下好的足迹。