C # WinForm Read the code of the XML file creation menu

Source: Internet
Author: User

The dynamic creation of menus makes the program more flexible, and this article dynamically creates menus based on reading the configuration menu items in the XML file 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 Frmaddmenu_load (object sender, EventArgs e)
{
MenuStrip MenuS = new MenuStrip ();
Menus.parent = this;
String xmlfilename = @ "f:\c# Project \winformmenu\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)
//{
ToolStripMenuItem mainmenu=new ToolStripMenuItem ();
Mainmenu.text = XML. attributes["Descript"]. Value;
MENUS.ITEMS.ADD (MainMenu);
if (XML. HasChildNodes)
//    {
Createsubmenubynode (XML, MainMenu);
//    }
//}
2: Create a menu based on XmlNode
XmlElement XML = xmlhelper.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>
Create a menu based on node recursive recursion
</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>
Create a menu recursively based on element node recursion
</summary>
<param name= "xml" ></param>
<param name= "MenuItem" ></param>
public void Createsubmenubyelement (XmlElement xml, ToolStripMenuItem menuItem)
{
foreach (XmlElement xel in XML. ChildNodes)
{
ToolStripMenuItem submenu = new ToolStripMenuItem ();
if (Xel. CHILDNODES.COUNT&GT;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:

Copy CodeThe code is as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<mangetvmenu descript= "Management" >
<add descript= "Add" >
<Employee> Staff </Employee>
<Deptment> Department </Deptment>
</Add>
<employeeinfo descript= "Employee Information" >
<BaseInfo> Personal basic Information </BaseInfo>
<StudyInfo> Education Information </StudyInfo>
<MarryInfo> Marriage Information </MarryInfo>
</EmployeeInfo>
<deptmentinfo descript= "Department info" >
<dept1> Department 1</dept1>
<dept2> Department 2</dept2>
</DeptmentInfo>
<moneymanage descript= "Payroll Management" >
<BaseMoney> Basic Salary </BaseMoney>
<OtherMoney> Performance Pay </OtherMoney>
</MoneyManage>
<apply descript= "Application" >
<AddWork> Overtime </AddWork>
<ApplyWeek> Leave </ApplyWeek>
<AddMoney> Pay </AddMoney>
<Another> other </Another>
</Apply>
</MangeTvMenu>

C # WinForm Read the code of the XML file creation menu

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.