This article describes the method of using XML to serialize the operation menu in C#. Share it for your reference. The specific analysis is as follows:
A previous article "C# Recursively Read XML Menu Data》Unable to use XML serialization to operate the menu, and I found that there is another problem, that is, if some annotation code is added before a menu node of the XML menu, it cannot be read. Now, it can be read easily after using XML serialization, so I will write it here.
The node code of the XML menu is as follows:
<?xml version="1.0" encoding="utf-8"?>
< xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:xsd="http:///2001/XMLSchema">
<Applications>
<Application ID ="OA" Text="OA Management System">
<Modules>
<Module ID="OA_System" Text="System">
<Menus>
<Menu ID="OA_System_UserManager" Text="Personnel Management" URL="System/UserManager/"> </Menu>
<Menu ID="OA_System_RoleManager" Text="RoleManager" URL="System/RoleManager/"></Menu>
<Menu ID="OA_System_LoginLog" Text="LoginLog" URL="System/Log/"></Menu>
<Menu ID="OA_System_OperateLog" Text="OperateLog" URL="System/Log/"></Menu>
</Menus>
</Module>
<Module ID="OA_TargetManage" Text="Target Management">
<Menus>
<Menu ID="OA_TargetManage_TargetSetup" Text="Target Setting" URL="OA/TargetManage/">
</Menu>
</Menus>
</Module>
</Modules>
</Application>
<Applications>
</>
There is a node here: Applications (application node), which can put multiple Applications in it, and each Application node only contains one Modules (module node), Modules have multiple Modules, and each Module has only one Menus (menu node), and Menus has multiple Menu. Each node has two common properties: ID and Text.
Therefore, here is a common attribute class: BaseAttribute. Remember to add the serialization identifier Serializable before, and the code is as follows:
[Serializable]
public class BaseAttribute
{
[XmlAttribute(AttributeName = "ID")]
public string ID { get; set; }
[XmlAttribute(AttributeName = "Text")]
public string Text { get; set; }
}
Each node has two classes, one is a list and the other is an entity. The entity class needs to inherit a public class, as follows:
[Serializable]
public class ApplicationList
{
public ApplicationList()
{
= new List<Application>();
}
[XmlElement(ElementName = "Application")]
public List<Application> Applications { get; set; }
}
[Serializable]
public class Application : BaseAttribute
{
public Application()
{
= new ModuleList();
}
[XmlElement(ElementName = "Modules")]
public ModuleList Modules { get; set; }
[XmlAttribute(AttributeName = "URL")]
public string URL { get; set; }
}
[Serializable]
public class ModuleList
{
public ModuleList()
{
= new List<Module>();
}
[XmlElement(ElementName = "Module")]
public List<Module> modules { get; set; }
}
[Serializable]
public class Module : BaseAttribute
{
public Module()
{
= "True";
= new MenuList();
}
[XmlElement(ElementName = "Menus")]
public MenuList Menus { get; set; }
[XmlAttribute(AttributeName = "Display")]
public string Display { get; set; }
[XmlAttribute(AttributeName = "URL")]
public string URL { get; set; }
}
[Serializable]
public class MenuList
{
public MenuList()
{
= new List<Menu>();
}
[XmlElement(ElementName = "Menu")]
public List<Menu> Menus { get; set; }
}
/// <summary>
/// Menu category
/// </summary>
[Serializable]
public class Menu : BaseAttribute
{
public Menu()
{
= false;
= false;
}
[XmlAttribute(AttributeName = "Popup")]
public bool Popup { get; set; }
[XmlAttribute(AttributeName = "Securityable")]
public bool Securityable { get; set; }
[XmlAttribute(AttributeName = "URL")]
public string URL { get; set; }
}
The following classes are used to manipulate XML, and the code is as follows:
[Serializable,XmlRoot("")]
public class ZCSoftPlateForm
{
public ZCSoftPlateForm()
{
= new ApplicationList();
}
[XmlElement(ElementName = "Applications")]
public ApplicationList Applications { get; set; }
}
/// <summary>
/// Operate XML classes
/// </summary>
public class LoadFoundationXml
{
private static ZCSoftPlateForm _FoundationObject;
static LoadFoundationXml()
{
if (_FoundationObject == null)
{
string path = + "";
if ((path))
{
_FoundationObject = <ZCSoftPlateForm>(path);
}
}
}
private LoadFoundationXml()
{
}
public static ZCSoftPlateForm PlateFormObject
{
get
{
return _FoundationObject;
}
}
}
Finally, there is a serialization operation class, as follows:
/// <summary>
/// Serialize XML classes
/// </summary>
public class Serialization
{
public static T ToObject<T>(string xmlFile)
{
FileStream stream = null;
T local = <T>();
try
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
stream = new FileStream(xmlFile, , , );
local = (T)(stream);
();
}
catch
{
while (stream != null)
{
();
break;
}
throw new Exception("Xml deserialization failed!");
}
return local;
}
}
This can be called in the background, there is no recursion here, as follows
private static ZCSoftPlateForm plateForm;
List<MenuTreeData> list = new List<MenuTreeData>();
plateForm = ;
//Use the operation XML class to read XML
var appList = ;
foreach (var application in appList)
{
var appData = new MenuTreeData();
= 0;
= 0;
= ;
= ;
= "Folder";
= 0;
= true;
= null;
= null;
= ;
= ;
= null;
= null;
= false;
= false;
(appData);
if (!=null)
{
foreach (var module in )
{
bool display = () == "true" ? true : false;
string parentItem = null;//Previous node ID
var modData = new MenuTreeData();
= 0;
= 0;
= ;
= ;
= "Folder";
= 0;
= display;
= null;
if (display)
{
parentItem = ;
}
= parentItem;
= ;
= ;
= ;
= ;
= false;
= false;
(modData);
if (!=null)
{
foreach (var menu in )
{
var mData = new MenuTreeData();
= 0;
= 0;
= ;
= ;
= "Menu";
= 0;
= true;
= ;
if (display)
{
/*
* If the Display property in the module to which this menu belongs is set to visible true
* (Note: If it is not set, it is visible by default), then the upper level of the menu is the ID of the Module
*/
= ;
}
else
{
/*If the Display property in the module to which this menu belongs is set to invisible false,
* Then the upper level of the menu is the ID of the Application
*/
= ;
}
= ;
= ;
= ;
= ;
= false;
= false;
(mData);
}
}
}
}
}
The menu entity class used:
/// <summary>
/// System menu
/// </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; }
}
I hope this article will be helpful to everyone's C# programming.