找不到文件

时间:2010-09-27 10:18:01

标签: asp.net xml vb.net menu submenu

我正在使用asp.net中的自定义菜单系统,根据从网站主菜单中选择的菜单项,动态填充水平菜单。

此第二个菜单是从网站根目录中的自定义XML文件填充的。 (有关详细信息,请参阅http://loganyoung.wordpress.com/2010/06/03/asp-net-horizontal-submenu-from-xml/。)

当我写这篇文章时,它确实有效,但我的开发环境已经改变,现在我收到一条错误,说无法找到XML文件。

这是我的代码:

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

这是我得到的错误:
Could not find a part of the path 'c:\windows\system32\inetsrc\~\Submenus.xml'.

Menu2只是一个完全空的<asp:Menu>控件,直接位于页面主菜单下。

有人可以告诉我,我做错了吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

XmlDocument.Load期待您提供虚拟路径的文件路径。尝试将其更改为:

doc.Load(Page.MapPath("~/Submenus.xml"))

答案 1 :(得分:0)

如果使用doc.Load(“〜/ Submenus.xml”),则此xml必须位于项目中。否则你必须使用server.mappath。