Introduction
The article will help you to generate XML file which at the end used by RadMenu to generate menu.
Using the code
If you have license of RadMenu which is product of Telerik, then you can use this code to generate XML with the help of LINQ provided by .NET.
LoadXmlString is the method of RadMenu which allow us to create menu from XML. And with the help of few classes, collection and LINQ we can create XML which full-fill our requirement. Here our goal is to create following menu using XML, off curse we will use LINQ, classes, collection...

Let's get familiar with the classes and code use to generate XML.
internal class MenuItem
{
public int ID { get; set; }
public List<MenuItem> MenuItems { get; set; }
public string Title { get; set; }
public string Url { get; set; }
public MenuItem()
{
ID = 0;
Title = string.Empty;
Url = "javascript:void(0);";
MenuItems = new List<MenuItem>();
}
}
internal class Menu
{
public List<MenuItem> MenuItems { get; set; }
}
Menu class will contains the MenuItem, and MenuItem can have sub-menus, thats why there is a collection of MenuItem inside MenuItem.
Lets create one menu and two sub-menu.
Menu m = new Menu();
MenuItem root1 = new MenuItem();
root1.Title = "My Profile";
MenuItem root2 = new MenuItem();
root2.Title = "Knowledge Base";
m.MenuItems.Add(root1);
m.MenuItems.Add(root2);
Your two root element are now ready. Now we will add first element under first root.
root1.MenuItems.Add(new MenuItem()
{
ID = 1,
Title = "forums.asp.net",
Url = "http://forums.asp.net/"
});
I used the feature of C# 3.0 which provides the Object Initialization Expressions, which allow us to initializes the value without overloading constructor, you can use default construction and just assign the value.
We added 2 root element and first node to first root element, now lets add sub element at second root.
MenuItem root2item1 = new MenuItem()
{
ID = 1,
Title = "Blog Posts",
MenuItems = new List<MenuItem>()
};
root2item1.MenuItems.Add(new MenuItem()
{
ID = 1,
Title = "Best way to generate XML using LINQ",
Url = "http://knowledgebaseworld.blogspot.com/2008/08/best-way-to-generate-xml-using-linq.html",
MenuItems = new List<MenuItem>()
});
root2.MenuItems.Add(root2item1);
Here you can see first we create sub-menu then added more menu into sub-menu, and at the end we attached it to our second root. This way you can go more inner more dipper.
Our menu m is ready, we can now generate the XML by static method GenerateMenu method of MyMenu class.
private static XElement GenerateMenu(Menu m)
{
var MenuXml =
new XElement("Menu",
from menuItem in m.MenuItems
select new XElement("Item", new XAttribute("Text", menuItem.Title), new XAttribute("Href", menuItem.Url),
GeterateSub(menuItem.MenuItems)));
return MenuXml;
}
private static XElement GeterateSub(List<MenuItem> list)
{
if (list.Count > 0)
{
var v = new XElement("Group",
from menuLookup in list
select new XElement("Item",
new XAttribute("Text", menuLookup.Title),
new XAttribute("Href", menuLookup.Url), GeterateSub(menuLookup.MenuItems)
));
return v;
}
return null;
}
The GenerateMenu method uses the XElement and XAttribute to create element and attributes. We need to generate following XML for RadMenu.
<Menu>
<Item Text="Item Name" Href="" />
<Item Text="Item Name" Href="" />
<Item Text="Item Name" Href="">
<Group>
<Item Text="Item Name" Href="" />
<Item Text="Item Name" Href="" />
</Group>
</Item>
</Menu>
Where Group mean sub-node, Item mean root node. XElement will create Item and Group element and XAttribute will create Text and Href attribute of respective element.
GenerateMenu method is responsible to create main root menu which is Menu tag, and the first root elements. After that there is one recursive method GeterateSub, which generates based on the list provided in argument, until the list is empty it will generate XElement or XAttribute.
Assign this XML generated by GenerateMenu method to the RadMenu.
rmTest.LoadXmlString(KnowledgeBase.MyMenu.GenerateMenu());