Create Dynamic Menu in WinForms

using System;
using System.Data;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}

private void MainForm_Load(object sender, EventArgs e)
{
CMenuEx menu = new CMenuEx();
menu.Path = @"D:\Menu.xml";//xml Content
var sPath = @"D:\Menu.xml";//xml Content
if (menu.FileExit())
{
menu.LoadAllMenu(sPath, toolStripContainer1);
// read xml To load the menu
}
else
{ MessageBox.Show("XML File loading failed !"); }
}
}

/// <summary>
/// Menu reading class
/// </summary>
public class CMenuEx
{
private string _Path;

/// <summary>
/// Set up XML Configuration file path
/// </summary>
public string Path
{
get { return _Path; }
set { _Path = value; }
}

/// <summary>
/// To determine whether the file exists
/// </summary>
/// <returns> File exists </returns>
public bool FileExit()
{
if (File.Exists(_Path))
{ return true; }
else return false;
}

/// <summary>
/// Load menu
/// </summary>
/// <param name="menuStrip"> Parent menu object </param>
public void LoadAllMenu(string sXmlPath, ToolStripContainer pToolStripContainer)
{
DataSet ds = new DataSet();
ds.ReadXml(sXmlPath, XmlReadMode.Auto);
string ToolStripPanelType = "TopToolStripPanel";
// Find all the first level menus
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); // binding ToolStrip
foreach (DataRowView rvMain in dvMenuOptions)
// Cycle to get the main menu
{
sParentLevel = rvMain["ParentLevel"].ToString();
ToolStripMenuItem tsItemParent = new ToolStripMenuItem();
tsItemParent.Text = rvMain["Text"].ToString();
tsItemParent.Name = rvMain["Name"].ToString();
tsTop.Items.Add(tsItemParent);// Add parent menu
// Find all submenu under the parent menu
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();
// Add a click event to the menu
itemSub.Click += new EventHandler(toolSubItem_Click);
// Menu response function
itemSub.Name = rvSub["Method"].ToString();
tsItemParent.DropDownItems.Add(itemSub);
}
}
}

// Custom message response function
private void toolSubItem_Click(object sender, EventArgs e)
{
// Creates an instance of a menu call method class
MenuMethod menuMethod = new MenuMethod();
Type type = menuMethod.GetType();
// Dynamic acquisition method object
MethodInfo mi = type.GetMethod(((ToolStripMenuItem)sender).Name);
// Calling the specified method
if (mi != null)
{
mi.Invoke(menuMethod, null);
}
}

/// <summary>
/// Menu method list class
/// </summary>
private class MenuMethod
{
public void New()
{
MessageBox.Show("New");
}

public void Open()
{
MessageBox.Show("Open");
}
}
}
}

XML File

<?xml version="1.0" encoding="UTF-8" ?>
<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></ShortCutKeys>
<Text> document toolbar </Text>
<Name>DocTool</Name>
<Image></Image>
<Expression></Expression>
<Assembly></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></ToolStripPanelType>
<ToolStripDisplayPosition></ToolStripDisplayPosition>
<TDTVisible>True</TDTVisible>
<ShortCutKeys>Ctrl+N</ShortCutKeys>
<Text> New map document </Text>
<Method>New</Method>
<Image>/img/New.ico</Image>
<Expression></Expression>
<Assembly></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></ToolStripPanelType>
<ToolStripDisplayPosition></ToolStripDisplayPosition>
<TDTVisible>True</TDTVisible>
<ShortCutKeys>Ctrl+O</ShortCutKeys>
<Text> open documents </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>

JSON Object

{
"Menus": {
"MenuOptions": [
{
"ID": "3766e9a2-7955-44eb-ad87-91ccb798baa7",
"ParentLevel": "3766e9a2-7955-44eb-ad87-91ccb798baa7",
"DisplayOrder": "1",
"ToolBarItemType": "ToolStripButton",
"ToolStripItemDisplayStyle": "ImageAndText",
"ToolStripPanelType": "TopToolStripPanel",
"ToolStripDisplayPosition": "1",
"TDTVisible": "True",
"ShortCutKeys": "",
"Text": " document toolbar ",
"Name": "DocTool",
"Image": "",
"Expression": "",
"Assembly": ""
},
{
"ID": "fd75638f-6c10-473d-b6e6-bdfd2c7931d6",
"ParentLevel": "3766e9a2-7955-44eb-ad87-91ccb798baa7",
"DisplayOrder": "0",
"ToolBarItemType": "ToolStripButton",
"ToolStripItemDisplayStyle": "Image",
"ToolStripPanelType": "",
"ToolStripDisplayPosition": "",
"TDTVisible": "True",
"ShortCutKeys": "Ctrl+N",
"Text": " New map document ",
"Method": "New",
"Image": "/img/New.ico",
"Expression": "",
"Assembly": ""
},
{
"ID": "9c6238d5-b47d-4b08-933c-ea7c74f6b586",
"ParentLevel": "3766e9a2-7955-44eb-ad87-91ccb798baa7",
"DisplayOrder": "1",
"ToolBarItemType": "ToolStripButton",
"ToolStripItemDisplayStyle": "Image",
"ToolStripPanelType": "",
"ToolStripDisplayPosition": "",
"TDTVisible": "True",
"ShortCutKeys": "Ctrl+O",
"Text": " open documents ",
"Method": "Open",
"Image": "/ico/open.ico",
"Expression": "Com.Linjon.ArcGIS.PlugIn.File.OpenDocCmd",
"Assembly": "Com.Linjon.ArcGIS.PlugIn.dll"
}
]
}
}

C# Class

public class MenuOption
{
public string ID { get; set; }
public string ParentLevel { get; set; }
public string DisplayOrder { get; set; }
public string ToolBarItemType { get; set; }
public string ToolStripItemDisplayStyle { get; set; }
public string ToolStripPanelType { get; set; }
public string ToolStripDisplayPosition { get; set; }
public string TDTVisible { get; set; }
public string ShortCutKeys { get; set; }
public string Text { get; set; }
public string Name { get; set; }
public string Image { get; set; }
public string Expression { get; set; }
public string Assembly { get; set; }
public string Method { get; set; }
}

public class Menus
{
public List<MenuOption> MenuOptions { get; set; }
}

public class RootObject
{
public Menus Menus { get; set; }
}