Generate Menus Using XML

Introduction

In this article, I am going to show how to easily generate menus using XML from the data source in Asp.net C#. By using this we can define the hierarchy separately in XML and use the same in the menus. Below is the real time code I have used in my project and I have attached the same in the attachment section.

Aspx page Code

  1. <asp:XmlDataSource ID="XmlDataSrc" runat="server" DataFile="App_Data/Tpms_Menus.xml" XPath="/menu/Admin/FirstlevelMenus"></asp:XmlDataSource>  
  2. <asp:Menu ID="Menu1" runat="server" DataSourceID="XmlDataSrc" Orientation="Horizontal" OnMenuItemClick="Menu1_MenuItemClick" Font-Names="Verdana" ForeColor="White" Width="800px" CssClass="MYMenuClass">  
  3.   
  4.     <DataBindings>  
  5.         <asp:MenuItemBinding DataMember="FirstlevelMenus" TextField="textdis" NavigateUrlField="urllink" />  
  6.         <asp:MenuItemBinding DataMember="secondlevelMenus" TextField="txtdis" NavigateUrlField="secondlevelurl" />  
  7.         <asp:MenuItemBinding DataMember="thirdlevelMenus" TextField="txtdis" NavigateUrlField="thirdlevelurl" />  
  8.     </DataBindings>  
  9.   
  10.   
  11. </asp:Menu>  
Note JFY

You can place the same in the .master page, if you have multiple pages in the project and want the menu to appear in all the pages.

XML Code 
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <menu>  
  3.   
  4.     <Admin>  
  5.         <FirstlevelMenus textdis="Users" urllink="~/TP_Admin.aspx">  
  6.             <secondlevelMenus txtdis="ViewUsers" secondlevelurl="ViewUsers.aspx">  
  7.   
  8.             </secondlevelMenus>  
  9.             <secondlevelMenus txtdis="UpdateUser" secondlevelurl="~/TP_Admin.aspx">  
  10.                 <thirdlevelMenus txtdis="checkthirdlevel" thirdlevelurl="">  
  11.                 </thirdlevelMenus>  
  12.             </secondlevelMenus>  
  13.             <secondlevelMenus txtdis="UnlockUser" secondlevelurl=""></secondlevelMenus>  
  14.             <secondlevelMenus txtdis="SetUserPrivelage" secondlevelurl=""></secondlevelMenus>  
  15.             <secondlevelMenus txtdis="ViewUsers" secondlevelurl=""></secondlevelMenus>  
  16.         </FirstlevelMenus>  
  17.   
  18.         <FirstlevelMenus textdis="Values" urllink="">  
  19.             <secondlevelMenus txtdis="AddBOQ" secondlevelurl="">  
  20.                 <thirdlevelMenus txtdis="ImportBOQ" thirdlevelurl=""></thirdlevelMenus>  
  21.             </secondlevelMenus>  
  22.             <secondlevelMenus txtdis="EditBOQ" secondlevelurl=""></secondlevelMenus>  
  23.         </FirstlevelMenus>  
  24.         <FirstlevelMenus textdis="Activity" urllink="">  
  25.             <secondlevelMenus txtdis="Activity_Groups" secondlevelurl="~/Estimation/CEO.aspx"> </secondlevelMenus>  
  26.             <secondlevelMenus txtdis="Activity_Items" secondlevelurl="/Estimation/Activity_Items.aspx"></secondlevelMenus>  
  27.         </FirstlevelMenus>  
  28.         <FirstlevelMenus textdis="Resources" urllink="">  
  29.             <secondlevelMenus txtdis="Materials" secondlevelurl="~/Estimation/Materials.aspx"></secondlevelMenus>  
  30.             <secondlevelMenus txtdis="Edittender" secondlevelurl="~/TPMS_ASPX/TSCM/Tender_Registry.aspx"></secondlevelMenus>  
  31.         </FirstlevelMenus>  
  32.     </Admin>  
  33.   
  34.   
  35. </menu>  
Output

Output

Conclusion

I hope the above information was useful. Kindly let me know your valuable feedback or thoughts.
Ebook Download
View all
Learn
View all