xml file to treeview with vb.net

Experts, I am having a very hard time making this work.  I have an xml file with submenus that I want to display in treeview, I think I have downloaded every recursive loop on the web that deals with transfering an xml file to treeview and none of them look like they should.  Could someone please help me with this?  I know the below  xml file can be converted to a tree view like hiarchy because I have seen it done on a asp.net webform  with the skmMenu control. So I dont know why its being a pain converting it to treeview.

Here is my xml file.
<?xml version="1.0" encoding="utf-8" ?>
<menu>
  <menuItem>
    <text>MSN Home</text>
    <url>http://www.Msn.aspx</url>
  </menuItem>
  <menuItem>
    <text>Google</text>
    <url>http://www.google.com</url>
  </menuItem>
  <menuItem>
    <text>Yahoo</text>
    <url>http://www.yahoo.com</url>
       <subMenu>
        <menuItem>
          <text>Yahoo MyMood</text>
        <url>http://music.yahoo.com/launchcast/moods.asp?userID=1918879999</url>
      </menuItem>
      <menuItem>
        <text>Yahoo Music</text>
        <url>http://music.yahoo.com/</url>
        </menuItem>
      <menuItem>
        <text>Yahoo Chat</text>
        <url>http://chat.yahoo.com/</url>
        </menuItem>
      <menuItem>
        <text>Yahoo Classified</text>
        <url>http://classifieds.yahoo.com/</url>
      </menuItem>
      </subMenu>
   </menuItem>
</menu>
tentavariousAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

prakash_prkCommented:
Try the following function

The following two functions are the core
FileTreeViewWithMenus
PopulateMenu

call the LoadTreeView to load the funtion.

Regards
Prakash
------------------------------------------------------------------------------------------------------------------------
Private Sub LoadTreeView()
        Dim xmlContent As String

        Dim sReader As New StreamReader("I:\Temp\tt.xml")
        xmlContent = sReader.ReadToEnd()
        sReader.Close()

        FileTreeViewWithMenus(TreeView1.Nodes, xmlContent)

End Sub
------------------------------------------------------------------------------------------------------------------------
    Private Sub FileTreeViewWithMenus(ByVal treeViewNodes As TreeNodeCollection, ByVal xmlContent As String)
        treeViewNodes.Clear()

        Dim treeviewNode As TreeNode
        Dim xmlDocument As New XmlDocument

        xmlDocument.LoadXml(xmlContent)

        Dim menuItems As XmlNodeList = xmlDocument.SelectNodes("//menu/menuItem")

        For Each menuItem As XmlElement In menuItems
            treeviewNode = New TreeNode
            PopulateMenu(menuItem, treeviewNode)
            treeViewNodes.Add(treeviewNode)
        Next

        xmlDocument = Nothing

    End Sub
------------------------------------------------------------------------------------------------------------------------
    Private Sub PopulateMenu(ByVal menuItem As XmlElement, ByVal treeviewNode As TreeNode)

        treeviewNode.Tag = menuItem("url").InnerText
        treeviewNode.Text = menuItem("text").InnerText

        If (Not menuItem("subMenu") Is Nothing) Then
            Dim childNode As TreeNode
            For Each childMenuItm As XmlElement In menuItem("subMenu").ChildNodes
                childNode = New TreeNode
                PopulateMenu(childMenuItm, childNode)
                treeviewNode.Nodes.Add(childNode)
            Next
        End If
    End Sub
------------------------------------------------------------------------------------------------------------------------
    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        Dim msg As String
        msg = "Selected Node :" & e.Node.Text & vbCrLf
        msg &= "Selected Value :" & e.Node.Tag.ToString
        MsgBox(msg)
    End Sub
------------------------------------------------------------------------------------------------------------------------
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tentavariousAuthor Commented:
Excellent work, but now how can I change the image index?  I have a imagelist tied to the control any tree node that contains  the <url></url> tag i want to change the image index to 2?
0
tentavariousAuthor Commented:
Almost got it working perfect, now i want to be able to click the link then open up the webpage, is this possible?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

prakash_prkCommented:
to open the web in IE

try this
Process.Start(URL)

regards
prakash
0
tentavariousAuthor Commented:
Seems to be working good except for when the tree gets built it automatically selects the first node how do i keep this from happening, and I having problems if there isnt a <url> tag.  Because not all the menus will be a link themselves, they will be just a selection for a submenu.
0
tentavariousAuthor Commented:
Ok, i got it working so that I am setting the focus to another control after menu gets built so that the Afterselect event doent fire right away, I am still having problems if i dont include a <url> tag under the <text> tag.  How do i fix this?
0
prakash_prkCommented:
change this line  
 treeviewNode.Tag = menuItem("url").InnerText
to

If( menuItem("url") is Nothing )
 treeviewNode.Tag = String.Empty
Else
 treeviewNode.Tag = .InnerText
End If

Regards
Prakash
0
tentavariousAuthor Commented:
Still getting the same error with the root level is not valid.
0
prakash_prkCommented:
can u post here a sample of the xml?
0
tentavariousAuthor Commented:
I got it to work by creating a new class with a public xmltextwriter, just wondering if there is a better way?
Public Class Xmlfile
    Public xmltext As String
    Public xmltxtwriter As XmlTextWriter
    Dim treehold As TreeView

   Sub New(ByVal xmlval As String)
        xmltext = xmlval
        Dim thold As System.Text.Encoding
        thold = thold.UTF8
        xmltxtwriter = New XmlTextWriter(xmltext, thold)
    End Sub

    Public Sub writexml( ByVal treev As TreeView)
        'writes the treeview xml file
        Try
            looplvl1(treev)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub looplvl1(ByVal tvmainmenu As TreeView)
        'loops through all parent nodes
        If tvmainmenu.Nodes.Count = 0 Then Exit Sub
        Dim thold As System.Text.Encoding
        thold = thold.UTF8
        With xmltxtwriter
            .Indentation = 4
            .IndentChar = " "
            .Formatting = Formatting.Indented
            .WriteStartDocument()
            .WriteComment("Treeview xml file")
            .WriteStartElement("menu")
        Dim shold As String
            For Each tnode As TreeNode In tvmainmenu.Nodes
                .WriteStartElement("menuItem")
                .WriteElementString("text", tnode.Text)
                .WriteElementString("url", tnode.Tag)
                loopother(tnode)
                .WriteEndElement()
            Next
            .WriteEndElement()
            .Flush()
            .Close()
        End With
    End Sub

    Private Sub loopother(ByVal PNode As TreeNode)
        'loops through all child nodes
        Dim TNode As TreeNode
        Dim shold As String
        Dim bool As Boolean = True
        For Each TNode In PNode.Nodes
            If bool Then
                xmltxtwriter.WriteStartElement("subMenu")
                shold = "write submenu"
                bool = Not bool
            End If
            xmltxtwriter.WriteStartElement("menuItem")
            xmltxtwriter.WriteElementString("text", TNode.Text)
            xmltxtwriter.WriteElementString("url", TNode.Tag)
            xmltxtwriter.WriteEndElement()
        Next
        If Not shold Is Nothing Then
            'write subitem end
            xmltxtwriter.WriteEndElement()
        End If
    End Sub
end Class
0
prakash_prkCommented:
nice work tentavarious.

regards
prakash
0
prakash_prkCommented:
do u try to write xml from the treeview?
0
tentavariousAuthor Commented:
Thanks, for helping me building the treeview from the xml, I did try writing the xml and it seems to work fine.  I am able to add, delete and edit branches in the tree and it wrote the xml accordingly.  Just wondering now, if you see any bugs in my code that may cause an issue down the road?
0
prakash_prkCommented:
thank you tentavarious
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.