Over a million developers have joined DZone.

Generate Menus Using XML

How to use XML and ASP.NET C# to easily generate menus for your web apps.

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.


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:

Aspx Page Code

<asp:XmlDataSource ID="XmlDataSrc" runat="server" DataFile="App_Data/Tpms_Menus.xml" XPath="/menu/Admin/FirstlevelMenus"></asp:XmlDataSource>  
<asp:Menu ID="Menu1" runat="server" DataSourceID="XmlDataSrc" Orientation="Horizontal" OnMenuItemClick="Menu1_MenuItemClick" Font-Names="Verdana" ForeColor="White" Width="800px" CssClass="MYMenuClass">  

        <asp:MenuItemBinding DataMember="FirstlevelMenus" TextField="textdis" NavigateUrlField="urllink" />  
        <asp:MenuItemBinding DataMember="secondlevelMenus" TextField="txtdis" NavigateUrlField="secondlevelurl" />  
        <asp:MenuItemBinding DataMember="thirdlevelMenus" TextField="txtdis" NavigateUrlField="thirdlevelurl" />  


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 

<?xml version="1.0" encoding="utf-8" ?>  

        <FirstlevelMenus textdis="Users" urllink="~/TP_Admin.aspx">  
            <secondlevelMenus txtdis="ViewUsers" secondlevelurl="ViewUsers.aspx">  

            <secondlevelMenus txtdis="UpdateUser" secondlevelurl="~/TP_Admin.aspx">  
                <thirdlevelMenus txtdis="checkthirdlevel" thirdlevelurl="">  
            <secondlevelMenus txtdis="UnlockUser" secondlevelurl=""></secondlevelMenus>  
            <secondlevelMenus txtdis="SetUserPrivelage" secondlevelurl=""></secondlevelMenus>  
            <secondlevelMenus txtdis="ViewUsers" secondlevelurl=""></secondlevelMenus>  

        <FirstlevelMenus textdis="Values" urllink="">  
            <secondlevelMenus txtdis="AddBOQ" secondlevelurl="">  
                <thirdlevelMenus txtdis="ImportBOQ" thirdlevelurl=""></thirdlevelMenus>  
            <secondlevelMenus txtdis="EditBOQ" secondlevelurl=""></secondlevelMenus>  
        <FirstlevelMenus textdis="Activity" urllink="">  
            <secondlevelMenus txtdis="Activity_Groups" secondlevelurl="~/Estimation/CEO.aspx"> </secondlevelMenus>  
            <secondlevelMenus txtdis="Activity_Items" secondlevelurl="/Estimation/Activity_Items.aspx"></secondlevelMenus>  
        <FirstlevelMenus textdis="Resources" urllink="">  
            <secondlevelMenus txtdis="Materials" secondlevelurl="~/Estimation/Materials.aspx"></secondlevelMenus>  
            <secondlevelMenus txtdis="Edittender" secondlevelurl="~/TPMS_ASPX/TSCM/Tender_Registry.aspx"></secondlevelMenus>  




I hope the above information was useful. Kindly let me know your valuable feedback or thoughts.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}