Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Bugsnag monitors application stability, so you can make data-driven decisions on whether you should be building new features, or fixing bugs. Learn more.

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:

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">  

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


</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 

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

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

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

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


</menu>

Output


Output
Conclusion

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

Monitor application stability with Bugsnag to decide if your engineering team should be building new features on your roadmap or fixing bugs to stabilize your application.Try it free.

Topics:
c# ,xml ,asp.net

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}