动态加载XML

这个案例是一个基于VS2005的WinForm菜单控件的例子,功能是从XML中动态加载菜单数据到MenuTrip控件上。这个案例中用到了XML,反射之类的技术。当然实用价值有限,仅供学习参考之用。

首先创建一个WinForm项目,在Form1上放置一个MenuTrip控件,将其命名为MainMenu。

在项目中添加一个XML文件,命名为Menu.xml,代码如下:

<xml version="1.0" encoding="utf-8" >

<Menus>

  <MainMenu id="10" title="文件">

    <SubMenu id="101">

      <Title>打开文件</Title>

</SubMenu>

    <SubMenu id="102">

      <Title>关闭文件</Title>

    </SubMenu>

  </MainMenu>

  <MainMenu id="11" title="编辑">

    <SubMenu id="111">

      <Title>剪切</Title>

    </SubMenu>

    <SubMenu id="112">

      <Title>复制</Title>

    </SubMenu>

    <SubMenu id="113">

      <Title>粘贴</Title>

    </SubMenu>

  </MainMenu>

</Menus>

接着往项目中再添加一个Menu.cs类,用来完成菜单的读取等相关操作,代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Xml;

using System.Reflection;

namespace WindowsFormsApplication1

{

public class Menu

    {

private string _Path;

/// <summary>

/// 设置XML配置文件t路径

/// </summary>

public string Path

{

get { return _Path; }

set { _Path = value; }

}

/// <summary>

/// 判D断文件t是否存在

/// </summary>

/// <returns>文件t是否存在</returns>

public bool FileExit()

        {

if (File.Exists(_Path))

            {

return true;

            }

else

            {

return false;

            }

        }

/// <summary>

/// 加载菜单

/// </summary>

/// <param name="menuStrip">母菜单对象</param>

public void LoadAllMenu(MenuStrip menuStrip)

        {

//读取XML配置文件t

XmlTextReader xmlReader = new XmlTextReader(_Path);

while (xmlReader.Read())

            {

//判D断是否循-环到MainMenu节点

if (!xmlReader.IsEmptyElement && xmlReader.Name == "MainMenu")

                {

//创建一级菜单项

ToolStripMenuItem toolItem = new ToolStripMenuItem();                   

//获取属性ID值

string id = xmlReader.GetAttribute("id");

                    toolItem.Name = "Item" + id;

//获取属性TITLE值

string title = xmlReader.GetAttribute("title");

                    toolItem.Text = title;

if (title != null && title.Length > 1)

                    {

//动¥态¬添¬ª加一项菜单

                        menuStrip.Items.Add(toolItem);

                    }

                }

//判D断是滞ª到子Á菜单节点

else if (!xmlReader.IsEmptyElement && xmlReader.Name == "SubMenu")

                {

//创建子Á菜单对象

ToolStripMenuItem toolSubItem = new ToolStripMenuItem();

string id = xmlReader.GetAttribute("id");

                    toolSubItem.Name = "Item" + id;

//获取子Á菜单树Â

XmlReader xmlSubRead = xmlReader.ReadSubtree();

MenuMethod menuMethod = new MenuMethod();

while (xmlSubRead.Read())

                    {

if(!xmlSubRead.IsEmptyElement && xmlSubRead.Name == "Title")

                        {

//添¬ª加子Á菜单的文字Á

                            toolSubItem.Text = xmlSubRead.ReadElementString();

//为a菜单添¬ª加单击Â事件t

                            toolSubItem.Click += new EventHandler(toolSubItem_Click);

                        }

//获取母菜单对象

ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem)menuStrip.Items["Item" + id.Substring(0, 2)];

//添¬ª加子Á菜单

                        toolStripMenuItem.DropDownItems.Add(toolSubItem);

                    }

                }

            }

        }

void toolSubItem_Click(object sender, EventArgs e)

        {

//创建菜单调Â用®方法类的实例y

MenuMethod menuMethod = new MenuMethod();

Type type = menuMethod.GetType();

//动¥态¬获取方法对象

MethodInfo mi = type.GetMethod(((ToolStripMenuItem)sender).Name);

//调Â用®指定方法

            mi.Invoke(menuMethod, null);

        }

    }

}

另外再添加一个类,命名为MenuMethod.cs,该方法定义了每个菜单项在加载后单击事件所调用的方法。代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace WindowsFormsApplication1

{

public class MenuMethod

    {

public void Item101()

        {

MessageBox.Show("方¤?法¤¡§1");

        }

public void Item102()

        {

MessageBox.Show("方¤?法¤¡§2");

        }

public void Item111()

        {

MessageBox.Show("方¤?法¤¡§3");

        }

public void Item112()

        {

MessageBox.Show("方¤?法¤¡§4");

        }

public void Item113()

        {

MessageBox.Show("方¤?法¤¡§5");

        }

    }

}

最后在Form1.cs的Load方法中填写如下代码:

private void Form1_Load(object sender, EventArgs e)

        {

Menu menu = new Menu();

            menu.Path = @"F:\visual studio 2010\Projects\动¡¥态¬?菜?单Ì£¤\WindowsFormsApplication1\Menu.xml";

if (menu.FileExit())

            {

                menu.LoadAllMenu(MainMenu);

            }

else

            {

MessageBox.Show("XML文?件t加¨®载?失º¡ì败㨹");

            }

        }

posted @ 2012-08-13 11:00  zhcnblog  阅读(548)  评论(0编辑  收藏  举报