Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

xml file to treeview with vb.net

Posted on 2006-04-20
14
Medium Priority
?
470 Views
Last Modified: 2011-10-03
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>
0
Comment
Question by:tentavarious
  • 7
  • 7
14 Comments
 
LVL 10

Accepted Solution

by:
prakash_prk earned 2000 total points
ID: 16505729
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
 

Author Comment

by:tentavarious
ID: 16507087
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
 

Author Comment

by:tentavarious
ID: 16507302
Almost got it working perfect, now i want to be able to click the link then open up the webpage, is this possible?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:prakash_prk
ID: 16507778
to open the web in IE

try this
Process.Start(URL)

regards
prakash
0
 

Author Comment

by:tentavarious
ID: 16507979
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
 

Author Comment

by:tentavarious
ID: 16508333
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
 
LVL 10

Expert Comment

by:prakash_prk
ID: 16531711
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
 

Author Comment

by:tentavarious
ID: 16533874
Still getting the same error with the root level is not valid.
0
 
LVL 10

Expert Comment

by:prakash_prk
ID: 16534390
can u post here a sample of the xml?
0
 

Author Comment

by:tentavarious
ID: 16536926
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
 
LVL 10

Expert Comment

by:prakash_prk
ID: 16541252
nice work tentavarious.

regards
prakash
0
 
LVL 10

Expert Comment

by:prakash_prk
ID: 16541436
do u try to write xml from the treeview?
0
 

Author Comment

by:tentavarious
ID: 16543980
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
 
LVL 10

Expert Comment

by:prakash_prk
ID: 16544066
thank you tentavarious
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question