using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml; 

public partial class _Default : System.Web.UI.Page
{

  private XmlDocument xmlMenu; // an xml representation of the menu

  /// <summary>
  /// Simulates checking for function accessibility
  /// </summary>
  private bool HasAccess( int FunctionID )
  {
    return ( FunctionID % 3 != 0 ); 
  }

  /// <summary>
  /// Constructs the menu for the RadMenu control in XML format, from another XML file. 
  /// </summary>
  private void ConstructMenu()
  {

    // Create and initialize xml representation of the menu. 
    xmlMenu = new XmlDocument();
    XmlDeclaration xmlDeclaration = xmlMenu.CreateXmlDeclaration( "1.0", "utf-8", null );
    XmlElement rootNode = xmlMenu.CreateElement( "Menu" );
    XmlElement grNode = xmlMenu.CreateElement( "Group" );
    grNode.SetAttribute( "Flow", "Horizontal" );
    rootNode.AppendChild( grNode );
    xmlMenu.InsertBefore( xmlDeclaration, xmlMenu.DocumentElement );
    xmlMenu.AppendChild( rootNode );
    
    // Load the xml file which defines the menu, but is not suitable for the RadMenu. This file 
    // simulates a menu definition gotten from an external source. 
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load( MapPath("SuperMenu.xml") );
    XmlNodeList xnl = xmlDoc.SelectNodes( "Modules/*" );

    // Add the top level menu items. Then create a group to contain the sublevel items. 
    foreach(XmlNode xn in xnl)
    {

      XmlElement parentNode = xmlMenu.CreateElement( "Item" );
      parentNode.SetAttribute( "PostBack", "False" );
      parentNode.SetAttribute( "Text", xn.Name );
      xmlMenu.DocumentElement.AppendChild( parentNode );

      XmlElement groupNode = xmlMenu.CreateElement( "Group" );
      groupNode.SetAttribute( "OffsetX", "10" );
      groupNode.SetAttribute( "Height", "100" );
      parentNode.AppendChild( groupNode );


      // Now add the children to this menu 
      foreach(XmlNode xnc in xn.ChildNodes)
      {
        int FunctionID = Convert.ToInt32 (xnc.Attributes["FunctionID"].Value) ;
        if(HasAccess( FunctionID ))
        {
          XmlElement childNode = xmlMenu.CreateElement( "Item" );
          childNode.SetAttribute( "Text", xnc.Attributes["DisplayName"].Value );
          groupNode.AppendChild( childNode );
        }
      }
      
    }
    // Assign the resulting Xml formatted string to the RadMenu
    RadMenu1.LoadXmlString(xmlMenu.InnerXml); 
  }

  protected void Page_Load( object sender, EventArgs e )
  {
    ConstructMenu(); 
  }
}

