I'm building a little XML driven menu. I'm not that familiar with XML objects yet but the code seems to be working ok. It is, however, on the ugly side. The key complicator is that the menu is dynamic - that is it needs to be changed depending on which page you are on, and a parent link needs to be flagged if a child link is selected. I could do it with JavaScript and DHTML but it needs to stay server side.
Has anyone got any quick suggestions on a better way to do this?
Note: LoadXmlFile is a function (not shown) that loads and validates an xml document. The output of which is XmlDocument type. The menuContent object is an ASP literal.
The code is as follows:
A sample xml file is as follows:
Has anyone got any quick suggestions on a better way to do this?
Note: LoadXmlFile is a function (not shown) that loads and validates an xml document. The output of which is XmlDocument type. The menuContent object is an ASP literal.
The code is as follows:
Code:
Dim xmlDoc As XmlDocument = LoadXmlFile(Server.MapPath("/menu.xml"))
Dim element As XmlElement
Dim subElement As XmlElement
Dim elements As XmlNodeList
Dim subElements As XmlNodeList
Dim output As New StringBuilder
Dim temp As New StringBuilder
Dim scriptName As String = Page.Request.ServerVariables("SCRIPT_NAME")
Dim elementSelected As Boolean
elements = xmlDoc.DocumentElement.SelectNodes("/menu/menuItem")
output.Append("<table>")
For Each element In elements
output.Append("<tr>")
elementSelected = False
If element.Attributes("url").Value.ToUpper = scriptName.ToUpper Then
elementSelected = True
End If
subElements = element.ChildNodes
If subElements.Count > 0 Then
temp.Remove(0, temp.Length)
temp.Append("<table>")
For Each subElement In subElements
temp.Append("<tr>")
If subElement.Attributes("url").Value.ToUpper = scriptName.ToUpper Then
elementSelected = True
temp.Append(String.Format("<td style=""padding-left: 30px;""><a class=""dynMenuOnText"" href=""{0}"">{1}</a></td>", subElement.Attributes("url").Value, subElement.Attributes("name").Value, ControlChars.CrLf))
Else
temp.Append(String.Format("<td style=""padding-left: 30px;""><a class=""dynMenuOffText"" href=""{0}"">{1}</a></td>", subElement.Attributes("url").Value, subElement.Attributes("name").Value, ControlChars.CrLf))
End If
temp.Append("</tr>")
Next
temp.Append("</table>")
End If
If elementSelected Then
If subElements.Count > 0 Then
output.Append(String.Format("<td><img class=""imageToLeft"" src=""images/menu_on.png"" alt=""""><a class=""dynMenuOn"" href=""{0}"">{1}</a>", element.Attributes("url").Value, element.Attributes("name").Value, ControlChars.CrLf))
output.Append(temp.ToString)
output.Append("</td>")
Else
output.Append(String.Format("<td><img class=""imageToLeft"" src=""images/menu_on.png"" alt=""""><a class=""dynMenuOn"" href=""{0}"">{1}</a></td>", element.Attributes("url").Value, element.Attributes("name").Value, ControlChars.CrLf))
End If
Else
output.Append(String.Format("<td><img class=""imageToLeft"" src=""images/menu_off.png"" alt=""""><a class=""dynMenuOff"" href=""{0}"">{1}</a></td>", element.Attributes("url").Value, element.Attributes("name").Value, ControlChars.CrLf))
End If
output.Append("</tr>")
Next
output.Append("</table>")
menuContent.Text=output.ToString
End If
A sample xml file is as follows:
Code:
<menu>
<menuItem name="Item Zero" url="/one.aspx" />
<menuItem name="Item One" url="/one.aspx">
<subItem name="Test" url="/test.aspx" />
<subItem name="Sub Item One" url="/default.aspx" />
<subItem name="Sub Item Two" url="/threetwo.aspx" />
</menuItem>
<menuItem name="Item Two" url="/about.aspx" />
<menuItem name="Item Three" url="/three.aspx">
<subItem name="Disclaimer" url="/disclaimer.aspx" />
</menuItem>
<menuItem name="Item Four" url="/four.aspx" />
</menu>