Public Class Main
Inherits StaticSiteMapProvider
Dim conn As New ConnectionVB
Dim _rootNode As SiteMapNode = Nothing
Dim _siteMapFileName As String
Dim SiteMapNodeName As String = "siteMapNode"
Public Sub DynamicSiteMapProvider()
End Sub
Public Shadows Function RootNode() As SiteMapNode
Return BuildSiteMap()
End Function
Public Overrides Sub Initialize(name As String, attributes As NameValueCollection)
//Me.Initialize(name, attributes)
_siteMapFileName = attributes("siteMapFile")
End Sub
Protected Overrides Function GetRootNodeCore() As SiteMapNode
Return RootNode()
End Function
Protected Overrides Sub clear()
SyncLock Me
_rootNode = Nothing
//Me.clear()
End SyncLock
End Sub
Public Overrides Function BuildSiteMap() As SiteMapNode
SyncLock Me
If _rootNode Is Nothing Then
clear()
Dim siteMapXml As XmlDocument = LoadSiteMapXml()
Dim rootElement As XmlElement = CType(siteMapXml.GetElementsByTagName(SiteMapNodeName)(0), XmlElement)
AddDynamicNodes(rootElement)
GenerateSiteMapNodes(rootElement)
End If
End SyncLock
Return _rootNode
End Function
Private Function LoadSiteMapXml() As XmlDocument
Dim siteMapXml As XmlDocument = New XmlDocument()
siteMapXml.Load(AppDomain.CurrentDomain.BaseDirectory + _siteMapFileName) //I got an error here
Return siteMapXml
End Function
Protected Sub AddDynamicNodes(rootElement As XmlElement)
Dim teams As XmlElement = AddDynamicChildElement(rootElement, "", "FootballTeams", "List of football team")
End Sub
Protected Function AddDynamicChildElement(parentElement As XmlElement, url As String, title As String, description As String) As XmlElement
Dim childElement As XmlElement = parentElement.OwnerDocument.CreateElement(SiteMapNodeName)
childElement.SetAttribute("url", url)
childElement.SetAttribute("title", title)
childElement.SetAttribute("description", description)
parentElement.AppendChild(childElement)
Return childElement
End Function
Protected Sub GenerateSiteMapNodes(rootElement As XmlElement)
_rootNode = GetSiteMapNodeFromElement(rootElement)
AddNode(_rootNode)
CreateChildNodes(rootElement, _rootNode)
End Sub
Protected Sub CreateChildNodes(parentElement As XmlElement, parentNode As SiteMapNode)
For Each XmlElement As XmlNode In parentElement.ChildNodes
If XmlElement.Name = SiteMapNodeName Then
Dim childNode As SiteMapNode = GetSiteMapNodeFromElement(CType(XmlElement, XmlElement))
AddNode(childNode, parentNode)
CreateChildNodes(CType(XmlElement, XmlElement), childNode)
End If
Next
End Sub
Protected Function GetSiteMapNodeFromElement(rootElement As XmlElement) As SiteMapNode
Dim newSiteMapNode As SiteMapNode
Dim url As String = rootElement.GetAttribute("url")
Dim title As String = rootElement.GetAttribute("title")
Dim description As String = rootElement.GetAttribute("description")
newSiteMapNode = New SiteMapNode(Me, (url + title).GetHashCode().ToString(), url, title, description)
Return newSiteMapNode
End Function
End Class
文我运行这段代码,它抛出一个异常,指出它找不到路径。
siteMapXml.Load(AppDomain.CurrentDomain.BaseDirectory + _siteMapFileName)
那是引发错误的代码行。
这是一个错误:
System.IO.DirectoryNotFoundException: Could not find a part of the path 'c:\Users\Julian\documents\visual studio 2012\WebSites\WebSite1\'.
我试图在那里放置一个断点并检查路径,但它已经是正确的。
我不知道这个错误的原因是什么。