c#使用Dataset讀取XML文件動態(tài)生成菜單的方法
本文實(shí)例講述了c#使用Dataset讀取XML文件動態(tài)生成菜單的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
Step 1:Form1 上添加一個ToolStripContainer控件
Step2:實(shí)現(xiàn)代碼
private void Form2_Load(object sender, EventArgs e)
{
CMenuEx menu = new CMenuEx();
string sPath = "D://Menu.xml";//xml的內(nèi)容
if (menu.FileExit())
{
menu.LoadAllMenu(sPath, toolStripContainer1);
//讀取xml來加載菜單
}
else
{ MessageBox.Show("XML文件加載失敗!"); }
}
/// <summary>
/// 菜單讀取類
/// </summary>
public class CMenuEx
{
private string _Path;
/// <summary>
/// 設(shè)置XML配置文件路徑
/// </summary>
public string Path
{
get { return _Path; }
set { _Path = value; }
}
/// <summary>
/// 判斷文件是否存在
/// </summary>
/// <returns>文件是否存在</returns>
public bool FileExit()
{
if (File.Exists(_Path))
{ return true; }
else return false;
}
/// <summary>
/// 加載菜單
/// </summary>
/// <param name="menuStrip">母菜單對象</param>
public void LoadAllMenu(string sXmlPath, ToolStripContainer pToolStripContainer)
{
DataSet ds = new DataSet();
ds.ReadXml(sXmlPath, XmlReadMode.Auto);
string ToolStripPanelType = "TopToolStripPanel";
//查找所有最初一級的菜單
DataView dvMenuOptions = new DataView(ds.Tables["MenuOptions"], "ParentLevel=ID and ToolStripPanelType='" + ToolStripPanelType + "'", "DisplayOrder Asc", DataViewRowState.CurrentRows);
string sParentLevel = "";
ToolStripPanel tspTop = pToolStripContainer.TopToolStripPanel;
tspTop.Dock = DockStyle.Top;
ToolStrip tsTop = new ToolStrip();
tspTop.Join(tsTop); //綁定ToolStrip
foreach (DataRowView rvMain in dvMenuOptions)
//循環(huán)得到主菜單
{
sParentLevel = rvMain["ParentLevel"].ToString();
ToolStripMenuItem tsItemParent = new ToolStripMenuItem();
tsItemParent.Text = rvMain["Text"].ToString();
tsItemParent.Name = rvMain["Name"].ToString();
tsTop.Items.Add(tsItemParent);//添加父菜單
//查找父菜單下的所有子菜單
DataView dvSub = new DataView(ds.Tables["MenuOptions"], "ParentLevel<>ID and ParentLevel='" + sParentLevel + "'", "DisplayOrder", DataViewRowState.CurrentRows);
foreach (DataRowView rvSub in dvSub)
{
ToolStripMenuItem itemSub = new ToolStripMenuItem();
itemSub.Text = rvSub["Text"].ToString() + " " + rvSub["ShortCutKeys"].ToString();
//為菜單添加單擊事件
itemSub.Click += new EventHandler(toolSubItem_Click);
//菜單響應(yīng)函數(shù)
itemSub.Name = rvSub["Method"].ToString();
tsItemParent.DropDownItems.Add(itemSub);
}
}
}
//自定義消息響應(yīng)函數(shù)
void toolSubItem_Click(object sender, EventArgs e)
{
//創(chuàng)建菜單調(diào)用方法類的實(shí)例
MenuMethod menuMethod = new MenuMethod();
Type type = menuMethod.GetType();
//動態(tài)獲取方法對象
MethodInfo mi = type.GetMethod(((ToolStripMenuItem)sender).Name);
//調(diào)用指定方法
if (mi != null)
{
mi.Invoke(menuMethod, null);
}
}
/// <summary>
/// 菜單的方法列表類
/// </summary>
class MenuMethod
{
public void New()
{
MessageBox.Show("New");
}
public void Open()
{
MessageBox.Show("Open");
}
}
}
附:xml內(nèi)容:
<?xml version="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#程序設(shè)計有所幫助。
- C#將Excel中的數(shù)據(jù)轉(zhuǎn)換成DataSet
- C#實(shí)現(xiàn)讀取DataSet數(shù)據(jù)并顯示在ListView控件中的方法
- C#實(shí)現(xiàn)利用泛型將DataSet轉(zhuǎn)為Model的方法
- C#通過DataSet讀寫xml文件的方法
- C#中DataSet轉(zhuǎn)化為實(shí)體集合類的方法
- C#使用DataSet Datatable更新數(shù)據(jù)庫的三種實(shí)現(xiàn)方法
- C#中遍歷DataSet數(shù)據(jù)集對象實(shí)例
- C# XML操作 代碼大全(讀XML,寫XML,更新,刪除節(jié)點(diǎn),與dataset結(jié)合等)
- C#實(shí)現(xiàn)DataSet內(nèi)數(shù)據(jù)轉(zhuǎn)化為Excel和Word文件的通用類完整實(shí)例
相關(guān)文章
C#使用Socket實(shí)現(xiàn)發(fā)送和接收圖片的方法
這篇文章主要介紹了C#使用Socket實(shí)現(xiàn)發(fā)送和接收圖片的方法,涉及C#操作socket發(fā)送與接收文件的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
C#使用SQL DataReader訪問數(shù)據(jù)的優(yōu)點(diǎn)和實(shí)例
今天小編就為大家分享一篇關(guān)于C#使用SQL DataReader訪問數(shù)據(jù)的優(yōu)點(diǎn)和實(shí)例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
C#多線程編程之使用ReaderWriterLock類實(shí)現(xiàn)多用戶讀與單用戶寫同步的方法
這篇文章主要介紹了C#多線程編程之使用ReaderWriterLock類實(shí)現(xiàn)多用戶讀與單用戶寫同步的方法,涉及C#多線程操作讀寫鎖定的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
C#設(shè)置程序開機(jī)啟動的實(shí)現(xiàn)示例
本文主要介紹了C#設(shè)置程序開機(jī)啟動的實(shí)現(xiàn)示例,可以通過修改注冊表將啟動信息寫入注冊表來實(shí)現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-01-01
C#多線程學(xué)習(xí)之(五)使用定時器進(jìn)行多線程的自動管理
這篇文章主要介紹了C#多線程學(xué)習(xí)之使用定時器進(jìn)行多線程的自動管理,實(shí)例分析了C#使用timer定時器類實(shí)現(xiàn)針對多線程的自動管理功能,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04
sqlserver備份還原數(shù)據(jù)庫功能封裝分享
這篇文章主要介紹了sqlserver備份還原數(shù)據(jù)庫功能封裝示例,需要的朋友可以參考下2014-03-03

