C # winform reading the xml creation menu

Source: Internet
Author: User

The dynamic menu creation greatly increases the flexibility of the program. This article dynamically creates a menu based on the configuration menu items in the read xml file. The Code is as follows: using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using WinformMenu. helper; using System. xml; namespace WinformMenu {public partial class FrmAddMenu: Form {public FrmAddMenu () {InitializeComponent ();} private void frmaddimethyl Nu_Load (object sender, EventArgs e) {MenuStrip menuS = new MenuStrip (); menuS. parent = this; string xmlFileName = @ "F: \ c # project \ WinformMenu \ XML \ menu. xml "; createMenu (xmlFileName, menuS);} public void createMenu (string xmlFileName, MenuStrip menuS) {// 1: Create a menu based on XmlElement // XmlNode node = xmlHelper. getXmlNode (xmlFileName, "// MangeTvMenu"); // foreach (XmlNode xml in node. childNodes) // {// OlStripMenuItem mainMenu = new ToolStripMenuItem (); // mainMenu. text = xml. attributes ["descript"]. value; // menuS. items. add (mainMenu); // if (xml. hasChildNodes) // {// CreateSubMenuByNode (xml, mainMenu); //} // 2: Create a menu XmlElement xml = xmlHelper Based on XmlNode. getXmlElement (xmlFileName); foreach (XmlElement xel in xml. childNodes) {ToolStripMenuItem mainMenu = new ToolStripMenuItem (); mainMenu. text = Xel. attributes ["descript"]. value; menuS. items. add (mainMenu); if (xml. hasChildNodes) {CreateSubMenuByElement (xel, mainMenu );}}} /// <summary> /// recursively create a menu based on the node /// </summary> /// <param name = "xNode"> </param> /// <param name = "menuItem"> </param> public void CreateSubMenuByNode (XmlNode xNode, toolStripMenuItem menuItem) {foreach (XmlNode xml in xNode. childNodes) {ToolStripMenuItem subMenu = new ToolStripMenuItem (); if (xml. childNodes. count> 1) {subMenu. text = xml. attributes ["descript"]. value; menuItem. dropDown. items. add (subMenu); CreateSubMenuByNode (xml, subMenu);} else {subMenu. text = xml. innerText; menuItem. dropDown. items. add (subMenu );}}} /// <summary> /// recursively create a menu based on the element node /// </summary> /// <param name = "xml"> </param> /// <param name = "menuItem"> </param> public void CreateSubMenuByE Lement (XmlElement xml, ToolStripMenuItem menuItem) {foreach (XmlElement xel in xml. childNodes) {ToolStripMenuItem subMenu = new ToolStripMenuItem (); if (xel. childNodes. count> 1) {subMenu. text = xel. attributes ["descript"]. value; menuItem. dropDown. items. add (subMenu); CreateSubMenuByElement (xel, subMenu);} else {subMenu. text = xel. innerText; menuItem. dropDown. items. add (subMenu );}}}/////////// ////////////////////////////////////////} The xml file format is as follows: <? Xml version = "1.0" encoding = "UTF-8"?> <MangeTvMenu descript = ""> <Add descript = ""> <Employee> Employee </Employee> <Deptment> Department </Deptment> </Add> <EmployeeInfo descript = "employee information"> <BaseInfo> basic personal information </BaseInfo> <StudyInfo> Education Information </StudyInfo> <MarryInfo> marital information </MarryInfo> </EmployeeInfo> <DeptmentInfo descript = "Department Information"> <dept1> department 1 </dept1> <dept2> Department 2 </dept2> </DeptmentInfo> <MoneyManage descript = "Salary Management"> <BaseMoney> basic salary </BaseMoney> <OtherMoney> performance salary </OtherMoney> </MoneyManage> <Apply descript = "Apply"> <AddWork> overtime </AddWork> <ApplyWeek> Leave </ApplyWeek> <AddMoney> salary increase </AddMoney> <Another> others </Another> </Apply> </MangeTvMenu>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.