posts - 64, comments - 387, trackbacks - 4

Using a Recursive Expression to Create an Html Menu

I find recursive expression to be really useful. They are particularly handy when dealing with parent child relationships. As a demonstration, see a potential method for generating a site map below:

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Text;

    5 

    6 namespace SiteMapDemo

    7 {

    8     class MenuItem

    9     {

   10         public Guid ID { get; set; }

   11         public Guid? ParentID { get; set; }

   12         public string Name { get; set; }

   13         public string Path { get; set; }

   14         public int Rank { get; set; }

   15     }

   16     class Program

   17     {

   18         static void Main(string[] args)

   19         {

   20             List<MenuItem> menu = new List<MenuItem>(new[]{

   21                 new MenuItem{ID = Guid.NewGuid(), Name = "First", ParentID=null, Path="/", Rank=0},

   22                 new MenuItem{ID = Guid.NewGuid(), Name = "Second", ParentID=null, Path="/second.aspx",Rank=1},

   23             });

   24             menu.AddRange(new[] {

   25                 new MenuItem{ID = Guid.NewGuid(), Name = "FirstSub", ParentID=menu[0].ID, Path="/firstsub.aspx",Rank=0},

   26                 new MenuItem{ID = Guid.NewGuid(), Name = "SecondSub", ParentID=menu[0].ID, Path="/secondsub.aspx",Rank=1},

   27                 });

   28             Func<List<MenuItem>, Guid?, string> renderMenu = null;

   29             renderMenu = (menus, Parent) =>

   30             {

   31                 var sub = menus.Where(m => m.ParentID == Parent).OrderBy(s => s.Rank).ToList();

   32                 if (sub.Count > 0)

   33                 {

   34                     StringBuilder sb = new StringBuilder();

   35                     sub.ForEach(s => { sb.Append(String.Format("<li><a href=\"{0}\">{1}</a>{2}</li>", s.Path, s.Name, renderMenu(menus, s.ID))); });

   36                     return sb.ToString();

   37                 }

   38                 return "";

   39             };

   40             Console.Write(renderMenu(menu, null));

   41             Console.ReadLine();

   42         }

   43     }

   44 }

The output:

<li><a href="http://www.charlesrcook.com/">First</a></li><li><a href="http://www.charlesrcook.com/firstsub.aspx">FirstSub</a></li><li><a href="/secondsub.aspx">SecondSub</a></li><li><a href="http://www.charlesrcook.com/second.aspx">Second</a></li>

Print | posted on Thursday, January 08, 2009 7:40 PM | Filed Under [ Web Programming ]

Feedback

Gravatar

# re: Using a Recursive Expression to Create an Html Menu

Good one here! There was a time when I wondered what people meant by recursive expressions, in my initial programming days when I didn’t know a code from a code! And HTML was an acronym they showed near gmail attachments! Do you know the best explanation I got when I asked about recursive expressions? It spoke of ‘regular expressions’ in our day-to-day life which are not really ‘regular’ in any way. It said that a regular expression has “itself in a ‘code’ part”! And this subsequently becomes a recursive regular expression! This is not without issues, but still, it is really useful!
5/12/2010 2:50 AM | Coursework
Gravatar

# re: Using a Recursive Expression to Create an Html Menu

ugg boots sale ugg boots salecheap uggcheap bootscheap ugg bootsUGG Argyle Knit BootsUGG Classic Mini Boots UGG Classic Tall Metallic Boots Ugg Boots UK
ugg boot on sale ugg sale ukugg boot sale uk UGG Argyle Knit BootsUGG Classic Cardy Bootscheap ugg boots
cheap bootscheap ugg ugg boots sale
12/18/2011 8:42 PM | Ugg Boots sale
Gravatar

# re: Using a Recursive Expression to Create an Html Menu

Hello!
I want to learn the HTML language. Please guide me which is the best institution of the HTML language.
12/28/2011 8:18 AM | Weight Loss Nashville
Gravatar

# re: Using a Recursive Expression to Create an Html Menu

I was no longer unwilling to go North; on the contrary, the proposed journey to a new world full of wonders kept me awake nights. I promised myself all sorts of fun and

adventures, though I was not entirely at rest in my mind touching the savages, and secretly resolved to go on board the ship -- the journey was to be made by sea -- with a

certain little brass pistol in my trousers-pocket, in case of any difficulty with the tribes when we landed at Boston.
12/30/2011 1:32 AM | Karen millen
Gravatar

# re: Using a Recursive Expression to Create an Html Menu

wedding dresses uk
wedding dresses uk sale
wedding dresses sale
wedding dresses uk 2012
wedding dresses uk online
wedding dresses on sale
cheap wedding dresses on sale
cheap wedding dresses sale
wedding dresses
wedding dress



Evening Dresses
Vintage Evening Dresses
Designer Evening Dresses
Cheap Cocktail Dresses
Short Cocktail Dresses
Long Evening Dresses
Evening Dresses on sale
Evening Dresses uk
1/3/2012 10:24 PM | weddingcall
Gravatar

# re: Using a Recursive Expression to Create an Html Menu

Thank you for sharing the information for us.I enjoyed your own article and definitely most of all really enjoyed reading.Thanks
1/6/2012 1:15 AM | Dental Marketing
Gravatar

# re: Using a Recursive Expression to Create an Html Menu


I stepped across the rug; he placed me&nbsp;Karen millen outlet&nbsp; square and straight before him. What a face he had, now that it was almost on a level with mine! what a great nose! and what a mouth! and what large prominent teeth!

'No sight so sad as that of a naughty child,' he began, 'especially a naughty little girl. Do you know where the wicked go after death?'

'They go to hell,' was my ready and orthodox answer.

'And what is hell? Can you tell me that?'

'A pit full of fire.'
1/7/2012 2:31 AM | Karen millen
Gravatar

# Pearl Hobbs

wedding dress 2011 Flower Girl Dresses bridal dresses evening dress patterns Cocktail Dresses wholesale wedding dresses column wedding dresses bride gowns evening dresses on sale bride gowns
1/11/2012 4:09 AM | wholesale wedding dress
Gravatar

# Kamboh

These days people are too much conscious about the results.
And this is the thing that people should have the warranty of any task before starting.
1/20/2012 11:41 AM | Norwalk Fitness Center
Gravatar

# re: Using a Recursive Expression to Create an Html Menu

"Of my childhood," repeated Otto, and they made themselves a way through the tumult.

They met with several young noblemen, relatives of Wilhelm, with&nbsp;Karen millen outlet&nbsp; the cousin who had written the verses for the Christmas tree; also several friends from the carouse, and the

company increased. They intended, like many others, to pass the night in the wood, and at midnight drink out of Kirsten Piil's well. "Only with the increasing darkness will it

become thoroughly merry here," thought they: but Otto had appointed to be in the city again toward evening. "Nothing will come out of that!" said the poet; "if you wish to

escape, we shall bind you fast to one of us."

"Then I carry him away with me on my back," replied Otto; "and still run toward the city. What shall I do here at night in the wood?"

"Be merry!" answered Wilhelm. "Come, give us no follies, or I shall grow restive."
2/1/2012 10:36 PM | Karenmillen

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 2 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET