Every website has a menu which is used to navigate from one page to another. The contents of the menu can come from different sources. In this article I will show how to create a simple menu using xml file and a DataList control.

Introduction:

Every website has a menu which is used to navigate from one page to another. The contents of the menu can come from different sources. In this article I will show how to create a simple menu using xml file and a DataList control.

Why use XML file?

The advantage of using the XML file is the low overhead and high performance. If you store the contents of the menu in the database than you will be accessing the database on every page request which is not a good practice. If the menu contents are stored in an XML file than you will only access the file when the contents of the file are changed.

Developing XML Menu: 

Let's see how we can use XML file as a menu. For this we will need a DataList control (You can also use DataRepeater) which will display the contents.

The DataList html code is given below. The DataList contains a ASP.NET Hyperlink control whose Text and NavigateUrl properties are set using the fields from the XML file. 

<asp:DataList id="AdminMenuList" runat="server" RepeatColumns="1" Font-Size="X-Small" RepeatLayout="Flow">
<ItemTemplate>
<asp:HyperLink Text = '<%# DataBinder.Eval(Container.DataItem,"Title") %>' NavigateUrl = '<%# DataBinder.Eval(Container.DataItem,"ItemUrl") %>' id="hlLinks" runat="server">
</asp:HyperLink>
</ItemTemplate>
</asp:DataList>

Let's see the XML File which contains the Title and the URL of the menu contents. As you can see in the XML file below there is the MenuItem tag which distinguishes one menu item from the other. There are also Title and ItemUrl tags which represents the title of the link and the actual url of the link respectively.

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

<Menu>

<MenuItem>

<Title>

Add User

</Title>

<ItemUrl>

http://azamsharp.net/Admin/AddUser.aspx

</ItemUrl>

</MenuItem>

<MenuItem>

<Title>

Add Location

</Title>

<ItemUrl>

http://azamsharp.net/Admin/AddLocation.aspx

</ItemUrl>

</MenuItem>

</Menu>

 

Let's define some constants that will hold the name and the path to the menu.

private const string MENU = "AdminMenu";

private const string MENU_FILE = "MenuAdmin.xml";

Now let's see the code that builds the menu. This code is called when there is no POSTBACK.

private DataSet BuildMenu()

{

DataSet dsMenu = new DataSet();

if(Cache[MENU] != null)

{

dsMenu = (DataSet) Cache[MENU];

}

else

{

dsMenu.ReadXml(Server.MapPath(MENU_FILE));

Cache.Insert(MENU,dsMenu,new System.Web.Caching.CacheDependency(Server.MapPath(MENU_FILE)));

}

return dsMenu;

}

All the code does is but the contents of the file in the Cache object so that we don't have to access the file again and again. Cache dependency is set to the XML file which means that whenever the file gets changes the Cache object will expire and the new contents are loaded from the file to the Cache object.

In this way you will only access the file when its changed and hence enhance the performance.

I hope you liked the article, happy coding!