Over a million developers have joined DZone.

Generate Menus Using XML

DZone's Guide to

Generate Menus Using XML

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

· Web Dev Zone ·
Free Resource

A true open source, API-first CMS — giving you the power to think outside the webpage. Try it for free.


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.

The New Standard for a Hybrid CMS: GraphQL Support, Scripting as a Service, SPA Support. Watch on-demand now.

c# ,xml ,asp.net

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}