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>