欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# winform读取xml文件创建菜单的代码

程序员文章站 2023-12-21 08:43:52
复制代码 代码如下:using system;using system.collections.generic;using system.componentmodel;us...
复制代码 代码如下:

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#工程\winformmenu\winformmenu\xml\菜单.xml";
            createmenu(xmlfilename, menus);
        }

       
        public void createmenu(string xmlfilename, menustrip menus)
        {  
            //1:根据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:根据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>
        /// 根据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>
        /// 根据元素节点递归创建菜单
        /// </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>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);
                }
            }
        }
 ///////////////////////////////////////////////////
    }
}

xml文件格式如下:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<mangetvmenu descript="管理">
  <add descript="添加">
    <employee>员工</employee>
    <deptment>部门</deptment>
  </add>
  <employeeinfo descript="员工信息">
    <baseinfo>个人基本信息</baseinfo>
    <studyinfo>学历信息</studyinfo>
    <marryinfo>婚姻信息</marryinfo>
  </employeeinfo>
  <deptmentinfo descript="部门信息">
    <dept1>部门1</dept1>
    <dept2>部门2</dept2>
  </deptmentinfo>
  <moneymanage descript="工资管理">
    <basemoney>基本工资</basemoney>
    <othermoney>绩效工资</othermoney>
  </moneymanage>
  <apply descript="申请">
    <addwork>加班</addwork>
    <applyweek>请假</applyweek>
    <addmoney>加薪</addmoney>
    <another>其他</another>
  </apply>
</mangetvmenu>

上一篇:

下一篇: