用C#与XML创建动态分层菜单

用C#与XML创建动态分层菜单1

Marking up the Menu

<menuItem>
   <hyperLink/>
   <name>Dollars & Sense</name>
   <menuItem>
      <hyperLink>strategy.aspx</hyperLink>
      <name>Annual Business Strategies</name>
   </menuItem>
   <menuItem>
      <hyperLink/>
      <name>Budget World</name>
      <menuItem>
         <hyperLink>/library/budget/default.aspx</hyperLink>
         <name>Infomemos</name>
      </menuItem>
      <menuItem>
         <hyperLink>/phonebooks/pdf/omb_phones.pdf</hyperLink>
         <name>Analysts</name>
      </menuItem>
      <menuItem>
         <hyperLink>/pp/budget/tocs/Policies_TOC.aspx</hyperLink>
         <name>Budget Policies</name>
      </menuItem>
   </menuItem>
</menuItem>

用C#与XML创建动态分层菜单2

Creation in Progress

public string CreateMenu(string startMenu,string file) {
    string strOutput = "";
    int i = 0;
    ArrayList startArray = new ArrayList();
    string strVariable = "";
    string strTemp = "";

    XmlDocument XMLDoc = new XmlDocument();
    try {
        XMLDoc.Load(file);
    }
    catch (Exception e) {
        strOutput = e.GetBaseException().ToString();
        return strOutput;
    }

    XmlNodeList nodeList = XMLDoc.DocumentElement.ChildNodes;

    foreach (XmlNode node in nodeList) {
        XmlNode currentNode = node;
        if (currentNode.HasChildNodes == true && currentNode.ChildNodes.Count>1) {
            _strCurrentMenu = startMenu + "_" + (i+1);
            string thisMenu = startMenu;
            if (currentNode.ChildNodes.Count>2) {
                strVariable = "<span id=\"" + thisMenu + "_span" + (i+1) + "\"class='cellOff' onMouseOver=\"stateChange('" 
                    + _strCurrentMenu +  "',this," + _intLevel + ")\" onMouseOut=\"stateChange('',this,'')\">" 
                    + _strImage + currentNode.ChildNodes[1].InnerText + "</span><br>\n";
                startArray.Add(strVariable);
                WalkTree(currentNode);
            } else {
                strVariable = "<span id=\"" + thisMenu + "_span" + (i+1) 
                    + "\" class='cellOff' onMouseOver=\"stateChange('',this,'');hideDiv(" + _intLevel 
                    + ")\"onMouseOut=\"stateChange('',this,'')\"onClick=\"location.href='" 
                    + currentNode.ChildNodes[0].InnerText + "'\">" + currentNode.ChildNodes[1].InnerText 
                    + "</span><br>\n";
                startArray.Add(strVariable);
            }
        }
        i++;
    }
    startArray.TrimToSize();
    _arrayNamesArray.Add(startMenu);
    for (i=0;i<startArray.Count;i++) {
        strTemp += startArray[i];
    }
    _arrayHolderArray.Add(strTemp);

    //Reverse Array order so we don't have to 
    //worry about the ZIndex of each div layer
    _arrayHolderArray.Reverse();
    _arrayNamesArray.Reverse();

    //Loop through arrays and write out divs and 
    //their individual span content items
    for (i=0;i<_arrayNamesArray.Count;i++) {
        strOutput += "<div id='" + 
            _arrayNamesArray[i].ToString() + "'class='clsMenu'>";
        strOutput += _arrayHolderArray[i].ToString();
        strOutput += "</div>\n";
    }
    _arrayHolderArray.Clear();
    _arrayNamesArray.Clear();
    if (_blnStaticMenus) {
        StreamWriter writer = 
            new StreamWriter(File.Open(_strSaveToFile, FileMode.OpenOrCreate, FileAccess.Write));
        writer.Write(strOutput);
        writer.Flush();
        if (writer !=null) writer.Close();
    }
    return strOutput;
} //CreateMenus

用C#与XML创建动态分层菜单3

Establishing Relationships

private void WalkTree(XmlNode node) {
    _intLevel += 1;
    string strVariable = "";
    string strTemp = "";
    ArrayList tempArray = new ArrayList();

    for (int j=2;j<node.ChildNodes.Count;j++) {
        XmlNode newNode = node.ChildNodes[j];

        if (newNode.HasChildNodes == true && newNode.ChildNodes.Count>2) {
            // Each node should have a 0=hyperlink and 1=text node so 
            // don't call the function again if there are just these 
            // children
            _strCurrentMenu += "_" + (j-1);
            string thisMenu = _strCurrentMenu.Substring(0, _strCurrentMenu.Length-2);
            strVariable = "<span id=\"" + thisMenu + "_span" + (j-1) 
                + "\" class='cellOff' onMouseOver=\"stateChange('" + _strCurrentMenu 
                + "',this," + _intLevel + ")\" onMouseOut=\"stateChange('',this,'')\">" 
                + _strImage + newNode.ChildNodes[1].InnerText + "</span><br>\n";
            tempArray.Add(strVariable);
            WalkTree(newNode);
        } else {
            strVariable = "<span id=\"" + _strCurrentMenu + "_span" + (j-1) 
            + "\" class='cellOff' onMouseOver=\"stateChange('',this,'');hideDiv(" 
            + _intLevel + ")\" onMouseOut=\"stateChange('',this,'')\" onClick=\"location.href='" 
            + newNode.ChildNodes[0].InnerText + "'\">" + newNode.ChildNodes[1].InnerText 
            + "</span><br>\n";
            tempArray.Add(strVariable);
        }
    }

    tempArray.TrimToSize();
    _arrayNamesArray.Add(_strCurrentMenu);
    for (int i=0;i<tempArray.Count;i++) {
        strTemp += tempArray[i];
    }
    _arrayHolderArray.Add(strTemp);
    _strCurrentMenu = _strCurrentMenu.Substring(0, _strCurrentMenu.Length-2);
    //Exiting function so go back to 
    //previous menu version
    _intLevel -= 1;
    tempArray.Clear();
} // WalkTree

用C#与XML创建动态分层菜单4

Last but not Least

<%
   // Create paths to xml files
   string filepath = Server.MapPath("menuItems.xml");
   string filepath2 = Server.MapPath("menuItems2.xml");
   string filepath3 = Server.MapPath("menuItems3.xml");

   // Following is constructor used to create a static menu
   // file in cases where the menu does not change often.
   // string saveToFile = Server.MapPath("test.aspx");
   // XmlMenu menuSave = new XmlMenu(true,saveToFile);
   // menuSave.CreateMenu("menu1",filepath);

   // Instantiate XmlMenu object and call CreateMenu method
   XmlMenu menu = new XmlMenu();
   Response.Write(menu.CreateMenu("menu1",filepath));
   Response.Write(menu.CreateMenu("menu2",filepath2));
   Response.Write(menu.CreateMenu("menu3",filepath3));
%>
Contributors: FHL