欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#使用Dataset讀取XML文件動(dòng)態(tài)生成菜單的方法

 更新時(shí)間:2015年05月08日 17:26:12   作者:PuttyTree  
這篇文章主要介紹了c#使用Dataset讀取XML文件動(dòng)態(tài)生成菜單的方法,涉及C#使用Dataset操作XML文件的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了c#使用Dataset讀取XML文件動(dòng)態(tài)生成菜單的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

Step 1:Form1 上添加一個(gè)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來(lái)加載菜單
 }
 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">母菜單對(duì)象</param>
 public void LoadAllMenu(string sXmlPath, ToolStripContainer pToolStripContainer)
 {
 DataSet ds = new DataSet();
 ds.ReadXml(sXmlPath, XmlReadMode.Auto);
 string ToolStripPanelType = "TopToolStripPanel";
 //查找所有最初一級(jí)的菜單
 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();
 //動(dòng)態(tài)獲取方法對(duì)象
 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>打開(kāi)文檔</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>

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • C#用匿名方法定義委托的實(shí)現(xiàn)方法

    C#用匿名方法定義委托的實(shí)現(xiàn)方法

    這篇文章主要介紹了C#用匿名方法定義委托的實(shí)現(xiàn)方法,涉及C#匿名方法與委托的相關(guān)使用技巧,需要的朋友可以參考下
    2015-06-06
  • C#連接MySQL的兩個(gè)簡(jiǎn)單代碼示例

    C#連接MySQL的兩個(gè)簡(jiǎn)單代碼示例

    這篇文章主要介紹了C#連接MySQL的簡(jiǎn)單代碼示例,需要的朋友可以參考下
    2017-06-06
  • 使用Log4net進(jìn)行日志記錄

    使用Log4net進(jìn)行日志記錄

    本文詳細(xì)講解了使用Log4net進(jìn)行日志記錄的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • C#中Linq的去重方式Distinct詳解

    C#中Linq的去重方式Distinct詳解

    本文詳細(xì)講解了C#中Linq的去重方式Distinct,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • C#中析構(gòu)函數(shù)、Dispose、Close方法的區(qū)別

    C#中析構(gòu)函數(shù)、Dispose、Close方法的區(qū)別

    本文詳細(xì)對(duì)比了C#中析構(gòu)函數(shù)、Dispose和Close方法的區(qū)別,三者都是釋放資源,本文介紹了他們各自的使用方法和使用場(chǎng)景,希望對(duì)大家有所幫助。
    2016-04-04
  • C#語(yǔ)法相比其它語(yǔ)言比較獨(dú)特的地方(三)

    C#語(yǔ)法相比其它語(yǔ)言比較獨(dú)特的地方(三)

    這篇文章主要介紹了C#語(yǔ)法相比其它語(yǔ)言比較獨(dú)特的地方(三),本文講解了在C++中允許從一個(gè)case貫穿到另一個(gè)case標(biāo)簽、as和is只會(huì)檢測(cè)待轉(zhuǎn)化類型的類型,而不會(huì)進(jìn)行其它操作等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • C#使用CancellationTokenSource 取消 Task的方法

    C#使用CancellationTokenSource 取消 Task的方法

    因?yàn)樯婕暗搅巳粘=?jīng)常會(huì)碰到的取消任務(wù)操作,本文主要介紹了C#使用CancellationTokenSource 取消 Task,文中通過(guò)代碼介紹的非常詳細(xì),感興趣的可以了解一下
    2022-02-02
  • C# 7.0之ref locals and returns(局部變量和引用返回)

    C# 7.0之ref locals and returns(局部變量和引用返回)

    這篇文章主要介紹了C# 7.0之ref locals and returns,即局部變量和引用返回,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 詳解C# Socket簡(jiǎn)單例子(服務(wù)器與客戶端通信)

    詳解C# Socket簡(jiǎn)單例子(服務(wù)器與客戶端通信)

    這篇文章主要介紹了詳解C# Socket簡(jiǎn)單例子(服務(wù)器與客戶端通信) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2016-12-12
  • 關(guān)于C#?dynamic裝箱問(wèn)題

    關(guān)于C#?dynamic裝箱問(wèn)題

    裝箱是將值類型轉(zhuǎn)換為類型對(duì)象或此值類型實(shí)現(xiàn)的任何接口類型的過(guò)程,裝箱和拆箱的概念是 C# 類型系統(tǒng)統(tǒng)一視圖的基礎(chǔ),其中任何類型的值都可以視為對(duì)象,這篇文章主要介紹了關(guān)于C#?dynamic裝箱引發(fā)的思考,需要的朋友可以參考下
    2022-05-05

最新評(píng)論