c#應用Dataset讀取XML文件靜態生成菜單的辦法。本站提示廣大學習愛好者:(c#應用Dataset讀取XML文件靜態生成菜單的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是c#應用Dataset讀取XML文件靜態生成菜單的辦法正文
本文實例講述了c#應用Dataset讀取XML文件靜態生成菜單的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:
Step 1:Form1 上添加一個ToolStripContainer控件
Step2:完成代碼
private void Form2_Load(object sender, EventArgs e) { CMenuEx menu = new CMenuEx(); string sPath = "D://Menu.xml";//xml的內容 if (menu.FileExit()) { menu.LoadAllMenu(sPath, toolStripContainer1); //讀取xml來加載菜單 } else { MessageBox.Show("XML文件加載掉敗!"); } } /// <summary> /// 菜單讀取類 /// </summary> public class CMenuEx { private string _Path; /// <summary> /// 設置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) //輪回獲得主菜單 { 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); //菜單呼應函數 itemSub.Name = rvSub["Method"].ToString(); tsItemParent.DropDownItems.Add(itemSub); } } } //自界說新聞呼應函數 void toolSubItem_Click(object sender, EventArgs e) { //創立菜單挪用辦法類的實例 MenuMethod menuMethod = new MenuMethod(); Type type = menuMethod.GetType(); //靜態獲得辦法對象 MethodInfo mi = type.GetMethod(((ToolStripMenuItem)sender).Name); //挪用指定辦法 if (mi != null) { mi.Invoke(menuMethod, null); } } /// <summary> /// 菜單的辦法列表類 /// </summary> class MenuMethod { public void New() { MessageBox.Show("New"); } public void Open() { MessageBox.Show("Open"); } } }
附:xml內容:
<?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#法式設計有所贊助。