Build Menu from XML files

Wednesday, July 01, 2015 Unknown 0 Comments

Build Dynamic menu's based on XML Content VB.net

This code snippet gets the element's value-of.
Private Function GetDocument(ByVal filesource As String) As XmlDocumentDim file As String = filesource'Dim file As String = "[YOURSITE PATH HERE. use server path ie. c:\vhost etc..]\" + filesourceDim key As String = ("__xmlCatalog:" + file.ToLower)Dim document As XmlDocument = CType(Context.Cache(key), XmlDocument)If (document Is Nothing) ThenDim dependency As CacheDependency = New CacheDependency(file)document = New XmlDocumentdocument.Load(file)' Context.Cache.Add(key, document, dependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, Nothing)End IfReturn documentEnd FunctionPrivate Function GetElement(ByVal Elementpath As String, ByVal file As String) As StringDim document As XmlDocument = GetDocument(file)Dim element As XmlElement = CType(document.SelectSingleNode(Elementpath), XmlElement)Return element.InnerTextEnd Function

Now use the GetElement.
TryDim files() As String = Directory.GetFiles("[YOURSITE PATH HERE point to directory with your xml files. use server path ie. c:\vhost etc..]", "*.xml")Dim fileobj As ObjectFor Each fileobj In filesDim strfile() As Stringstrfile = fileobj.ToString.Split("\")Dim strxml As String = strfile(strfile.Length - 1).ToStringDim menuitem As New System.Web.UI.WebControls.MenuItem(GetElement("DOC/TITLE", fileobj.ToString), "", "", "~/Reviews.aspx?" + strxml)Me.Menu1.Items(1).ChildItems.Add(menuitem)NextCatch ex As ExceptionEnd Try


This is based on reviews being in the second spot on the menu.
Example http:\\www.treetopbaby.ca
me.menu1.items(1) is the Review menu item. The ChildItem is the XML docment. The Title is pulled to place on the menu. The xml file is added to the URL string for the navURL.

xml example structure is

DOC
- TITLE

0 comments :

Powered by Blogger.