C#使用XML序列化操作菜单的方法
本篇章节讲解C#使用XML序列化操作菜单的方法。分享给大家供大家参考。具体分析如下: 之前的一篇文章《C#递归读取XML菜单数据的方法》没使用XML序列化来操作菜单,而且发现那还有一个问题,就是在XML菜单的某个菜单节点前加上一些注释代码的就不能读取,现在使用XML序列化后可以很方便的读取,故在此写一写。 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> <Applications> </ZCSoft.Net> 这里面有一个节点:Applications(应用程序节点),里面可以放多个Application,而每个Application节点里面只包含一个Modules(模块节点),Modules有多个Module,每个Module又只有一个Menus(菜单节点),而Menus里有多个Menu。而每个节点都有两个公共的属性:ID和Text。 故这里写一个公共的属性类:BaseAttribute,前面记得加上序列化标识Serializable,代码如下: 复制代码 代码如下: [Serializable] public class BaseAttribute { [XmlAttribute(AttributeName = "ID")] public string ID { get; set; } [XmlAttribute(AttributeName = "Text")] 每个节点都有两个类,一个是列表,一个是实体,实体类需继承公共的类,如下: 复制代码 代码如下: [Serializable] public class ApplicationList { public ApplicationList() { this.Applications = new List<Application>(); } [XmlElement(ElementName = "Application")] public List<Application> Applications { get; set; } } [Serializable] [XmlAttribute(AttributeName = "URL")]
[Serializable] [XmlAttribute(AttributeName = "Display")] [XmlAttribute(AttributeName = "URL")]
/// <summary> [XmlAttribute(AttributeName = "Popup")] [XmlAttribute(AttributeName = "Securityable")] [XmlAttribute(AttributeName = "URL")] 下面几个类是用于操作XML的,代码如下: 复制代码 代码如下: [Serializable,XmlRoot("ZCSoft.Net")] public class ZCSoftPlateForm { public ZCSoftPlateForm() { this.Applications = new ApplicationList(); } [XmlElement(ElementName = "Applications")] public ApplicationList Applications { get; set; } } /// <summary> public static ZCSoftPlateForm PlateFormObject 最后就是一个序列化操作类,如下: 复制代码 代码如下: /// <summary> /// 序列化XML类 /// </summary> public class Serialization { public static T ToObject<T>(string xmlFile) { FileStream stream = null; T local = Activator.CreateInstance<T>(); try { XmlSerializer serializer = new XmlSerializer(typeof(T)); stream = new FileStream(xmlFile,FileMode.Open,FileAccess.Read,FileShare.Read); local = (T)serializer.Deserialize(stream); stream.Close(); } catch { while (stream != null) { stream.Close(); break; } throw new Exception("Xml deserialization failed!"); } return local; } } 在后台可以这样调用,这里没用递归,如下 复制代码 代码如下: private static ZCSoftPlateForm plateForm; List<MenuTreeData> list = new List<MenuTreeData>(); plateForm = LoadFoundationXml.PlateFormObject; //使用操作XML类来读取XML if (application.Modules!=null) modData.ParentItem = parentItem; if (module.Menus!=null) 使用到的菜单实体类: 复制代码 代码如下: /// <summary> /// 系统菜单 /// </summary> public class MenuTreeData { public int ItemId { get; set; } public int TemplateId { get; set; } 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; } public string ParentItem { get; set; } public string ApplicationCode { get; set; } public string ApplicationName { get; set; } public string ModuleCode { get; set; } public string ModuleName { get; set; } public bool Securityable { get; set; } public bool Popup { get; set; } 希望本文所述对大家的C#程序设计有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |