views:

28

answers:

2

Hi!

I'm working on a custom menu system in asp.net that populates a horizontal menu on the fly based on which menu item is selected from the website's main menu.

This 2nd menu is populated from a custom XML file in the website's root directory. (See http://loganyoung.wordpress.com/2010/06/03/asp-net-horizontal-submenu-from-xml/ for details).

At the time I'd written that post, it did work, but my development environment has changed and now I'm getting an error saying that the XML file can't be found.

Here's my code:

Imports System.Xml

Partial Class Site
    Inherits System.Web.UI.MasterPage


    Protected Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles Menu1.MenuItemClick
        Select Case e.Item.Value.ToString
            Case "Team"
                Dim doc As New XmlDocument
                doc.Load("~/Submenus.xml")

                Dim NameNodes As XmlNodeList = doc.SelectNodes("/TeamMenu/item/name")
                Dim URLNodes As XmlNodeList = doc.SelectNodes("/TeamMenu/item/url")

                If NameNodes.Count = URLNodes.Count Then
                    For i As Integer = 0 To NameNodes.Count - 1
                        Dim m As New MenuItem
                        m.Text = NameNodes.Item(i).FirstChild.InnerText
                        m.NavigateUrl = URLNodes.Item(i).FirstChild.InnerText

                        Menu2.Items.Add(m)
                    Next
                End If
            Case "Investments"
                Dim doc As New XmlDocument
                doc.Load("~/Submenus.xml")

                Dim NameNodes As XmlNodeList = doc.SelectNodes("/InvestmentsMenu/item/name")
                Dim URLNodes As XmlNodeList = doc.SelectNodes("/InvestmentsMenu/item/url")

                If NameNodes.Count = URLNodes.Count Then
                    For i As Integer = 0 To NameNodes.Count - 1
                        Dim m As New MenuItem
                        m.Text = NameNodes.Item(i).FirstChild.InnerText
                        m.NavigateUrl = URLNodes.Item(i).FirstChild.InnerText

                        Menu2.Items.Add(m)
                    Next
                End If
            Case "Social Responsibility"
                Dim doc As New XmlDocument
                doc.Load("~/Submenus.xml")

                Dim NameNodes As XmlNodeList = doc.SelectNodes("/InvestmentsMenu/item/name")
                Dim URLNodes As XmlNodeList = doc.SelectNodes("/InvestmentsMenu/item/url")

                If NameNodes.Count = URLNodes.Count Then
                    For i As Integer = 0 To NameNodes.Count - 1
                        Dim m As New MenuItem
                        m.Text = NameNodes.Item(i).FirstChild.InnerText
                        m.NavigateUrl = URLNodes.Item(i).FirstChild.InnerText

                        Menu2.Items.Add(m)
                    Next
                End If
        End Select
    End Sub
End Class

And here's the error I'm getting:
Could not find a part of the path 'c:\windows\system32\inetsrc\~\Submenus.xml'.

Menu2 is just a completely empty <asp:Menu> control directly under the main menu on the page.

Can someone tell me what I'm doing wrong please?

Thanks in advance.

+1  A: 

XmlDocument.Load is expecting a file path where you are providing a virtual path. Try changing it to this:

doc.Load(Page.MapPath("~/Submenus.xml"))
Andy Rose
That killed the error, but the submenu doesn't display. Might have something to do with the XML, I'm going to look into it now. Your answer did remind me of Server.MapPath() which would also work in this case (and FYI, you're 1 `)` short ^.^)
Logan Young
A: 

If you use doc.Load("~/Submenus.xml") this xml must be in your project. Otherwise you have to use server.mappath.

kad1r