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

Posted 采菊东篱下,悠然见南山!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# winform读取xml文件创建菜单的代码相关的知识,希望对你有一定的参考价值。

动态创建菜单使得程序灵活性大大增加,本文根据读取xml文件中的配置菜单项来动态创建菜单,代码如下

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>

以上是关于c# winform读取xml文件创建菜单的代码的主要内容,如果未能解决你的问题,请参考以下文章

在c#的winform 下怎么读取 xml文件中的数据?

c#读取xml文件配置文件Winform及WebForm-Demo具体解释

C#中winform使用相对路径读取文件的方法

C#读取xml为null

C# winform 如何直接读取HTML语言后打开该网页

c# winform 如何读取 listview里的每一条数据