views:

40

answers:

1

I am trying to create a hierarchical xml based menu in MVC.NET

<?xml version="1.0" encoding="utf-8" ?>
<NavigationMenu id="1" Name="myMenu" Lang="EN">
  <NavigationMenuGroup Header="Home" Name="header1" Link="/home" />
  <NavigationMenuGroup Header="Manage" Name="header2" Link="/options" />
  <NavigationMenuGroup Header="About" Name="header3"  Link="/About" >
    <NavigationMenuItem Header="Test1" Name="header5"  Link="/page1" />
    <NavigationMenuItem Header="Test2" Name="header6"  Link="/page2" />
  </NavigationMenuGroup>
</NavigationMenu>

I pan for the masterpage to Render a Partial menu (NavMenu.ascx), the data will be fed to the masterpage by a Custom BaseController.

The NavMenu.ascx will produce code similar to following (easy to plugin jQuery or simlar).

 <ul id="menu">
   <li>
      <h2><a href="/home">Home</a></h2>
   </li>

   <li>
      <h2><a href="/options">Manage</a></h2>
   </li>

   <li>
      <h2><a href="/about">About</a></h2>

          <li>
             <h2><a href="/page2">Test1</a></h2>
          </li>
          <li>
             <h2><a href="/page1">Test2</a></h2>
          </li>
   </li>
</ul>

I also have setup a set of classes to Deserialize.

    namespace MyMVC.Helpers
    {
        public class XmlSerializerHelper<T>
        {
            public Type _type;

            public XmlSerializerHelper()
            {
                _type = typeof(T);
            }

            public void Save(string path, object obj)
            {
                using (TextWriter textWriter = new StreamWriter(path))
                {
                    XmlSerializer serializer = new XmlSerializer(_type);
                    serializer.Serialize(textWriter, obj);
                }
            }

            public T Read(string path)
            {
                T result;
                using (TextReader textReader = new StreamReader(path))
                {
                    XmlSerializer deserializer = new XmlSerializer(_type);
                    result = (T)deserializer.Deserialize(textReader);
                }
                return result;
            }
        }
}

I am not sure what is the best way to do this without breaking the MVC principals?

+1  A: 

Have a look at MvcSiteMap provider that works also with XML

Gregoire
this is useful, but Im looking for a neat way to hand roll this :)
Desiny