c#使用Dataset读取XML文件动态生成菜单的方法
本文实例讲述了c#使用Dataset读取XML文件动态生成菜单的方法。分享给大家供大家参考。具体实现方法如下:
Step1:Form1上添加一个ToolStripContainer控件
Step2:实现代码
privatevoidForm2_Load(objectsender,EventArgse)
{
CMenuExmenu=newCMenuEx();
stringsPath="D://Menu.xml";//xml的内容
if(menu.FileExit())
{
menu.LoadAllMenu(sPath,toolStripContainer1);
//读取xml来加载菜单
}
else
{MessageBox.Show("XML文件加载失败!");}
}
///<summary>
///菜单读取类
///</summary>
publicclassCMenuEx
{
privatestring_Path;
///<summary>
///设置XML配置文件路径
///</summary>
publicstringPath
{
get{return_Path;}
set{_Path=value;}
}
///<summary>
///判断文件是否存在
///</summary>
///<returns>文件是否存在</returns>
publicboolFileExit()
{
if(File.Exists(_Path))
{returntrue;}
elsereturnfalse;
}
///<summary>
///加载菜单
///</summary>
///<paramname="menuStrip">母菜单对象</param>
publicvoidLoadAllMenu(stringsXmlPath,ToolStripContainerpToolStripContainer)
{
DataSetds=newDataSet();
ds.ReadXml(sXmlPath,XmlReadMode.Auto);
stringToolStripPanelType="TopToolStripPanel";
//查找所有最初一级的菜单
DataViewdvMenuOptions=newDataView(ds.Tables["MenuOptions"],"ParentLevel=IDandToolStripPanelType='"+ToolStripPanelType+"'","DisplayOrderAsc",DataViewRowState.CurrentRows);
stringsParentLevel="";
ToolStripPaneltspTop=pToolStripContainer.TopToolStripPanel;
tspTop.Dock=DockStyle.Top;
ToolStriptsTop=newToolStrip();
tspTop.Join(tsTop);//绑定ToolStrip
foreach(DataRowViewrvMainindvMenuOptions)
//循环得到主菜单
{
sParentLevel=rvMain["ParentLevel"].ToString();
ToolStripMenuItemtsItemParent=newToolStripMenuItem();
tsItemParent.Text=rvMain["Text"].ToString();
tsItemParent.Name=rvMain["Name"].ToString();
tsTop.Items.Add(tsItemParent);//添加父菜单
//查找父菜单下的所有子菜单
DataViewdvSub=newDataView(ds.Tables["MenuOptions"],"ParentLevel<>IDandParentLevel='"+sParentLevel+"'","DisplayOrder",DataViewRowState.CurrentRows);
foreach(DataRowViewrvSubindvSub)
{
ToolStripMenuItemitemSub=newToolStripMenuItem();
itemSub.Text=rvSub["Text"].ToString()+""+rvSub["ShortCutKeys"].ToString();
//为菜单添加单击事件
itemSub.Click+=newEventHandler(toolSubItem_Click);
//菜单响应函数
itemSub.Name=rvSub["Method"].ToString();
tsItemParent.DropDownItems.Add(itemSub);
}
}
}
//自定义消息响应函数
voidtoolSubItem_Click(objectsender,EventArgse)
{
//创建菜单调用方法类的实例
MenuMethodmenuMethod=newMenuMethod();
Typetype=menuMethod.GetType();
//动态获取方法对象
MethodInfomi=type.GetMethod(((ToolStripMenuItem)sender).Name);
//调用指定方法
if(mi!=null)
{
mi.Invoke(menuMethod,null);
}
}
///<summary>
///菜单的方法列表类
///</summary>
classMenuMethod
{
publicvoidNew()
{
MessageBox.Show("New");
}
publicvoidOpen()
{
MessageBox.Show("Open");
}
}
}
附:xml内容:
<?xmlversion="1.0"encoding="GB2312"?> <Menus> <MenuOptions> <ID>3766e9a2-7955-44eb-ad87-91ccb798baa7</ID> <ParentLevel>3766e9a2-7955-44eb-ad87-91ccb798baa7</ParentLevel> <DisplayOrder>1</DisplayOrder> <ToolBarItemType>ToolStripButton</ToolBarItemType> <ToolStripItemDisplayStyle>ImageAndText</ToolStripItemDisplayStyle> <ToolStripPanelType>TopToolStripPanel</ToolStripPanelType> <ToolStripDisplayPosition>1</ToolStripDisplayPosition> <TDTVisible>True</TDTVisible> <ShortCutKeys/> <Text>文档工具栏</Text> <Name>DocTool</Name> <Image/> <Expression/> <Assembly/> </MenuOptions> <MenuOptions> <ID>fd75638f-6c10-473d-b6e6-bdfd2c7931d6</ID> <ParentLevel>3766e9a2-7955-44eb-ad87-91ccb798baa7</ParentLevel> <DisplayOrder>0</DisplayOrder> <ToolBarItemType>ToolStripButton</ToolBarItemType> <ToolStripItemDisplayStyle>Image</ToolStripItemDisplayStyle> <ToolStripPanelType/> <ToolStripDisplayPosition/> <TDTVisible>True</TDTVisible> <ShortCutKeys>Ctrl+N</ShortCutKeys> <Text>新建地图文档</Text> <Method>New</Method> <Image>/img/New.ico</Image> <Expression/> <Assembly/> </MenuOptions> <MenuOptions> <ID>9c6238d5-b47d-4b08-933c-ea7c74f6b586</ID> <ParentLevel>3766e9a2-7955-44eb-ad87-91ccb798baa7</ParentLevel> <DisplayOrder>1</DisplayOrder> <ToolBarItemType>ToolStripButton</ToolBarItemType> <ToolStripItemDisplayStyle>Image</ToolStripItemDisplayStyle> <ToolStripPanelType/> <ToolStripDisplayPosition/> <TDTVisible>True</TDTVisible> <ShortCutKeys>Ctrl+O</ShortCutKeys> <Text>打开文档</Text> <Method>Open</Method> <Image>/ico/open.ico</Image> <Expression>Com.Linjon.ArcGIS.PlugIn.File.OpenDocCmd</Expression> <Assembly>Com.Linjon.ArcGIS.PlugIn.dll</Assembly> </MenuOptions> </Menus>
希望本文所述对大家的C#程序设计有所帮助。