我正在使用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'.
菜單2僅僅是一個完全空<asp:Menu>
控制直接在頁面上的主菜單下。
有人可以告訴我我做錯了嗎?
在此先感謝。
這樣會導致錯誤消失,但子菜單不會顯示。可能與XML有關,我現在要研究它。你的回答確實讓我想起了Server.MapPath(),它也適用於這種情況(和FYI,你是'''''^^^) – 2010-09-27 10:41:15