XML in Treeview

OK , I have looked and still can't find the right stuff.

SO I am working on this project, that takes an XML record and displays only the parent node in the treeview. That part I got but where my problem is when you click on the parent node it is supposed to navigate to a web page related to the node. Also by hoveing over the node I am supposed to have a tooltip description appear.My XML is as follows....

<title>Herbs</title>
<link>C:\Documents and Settings\HP_Owner\My Documents\Dominion\Content\Database\herbs.html</link>
<description>a book on herbs</description>

In the tree view only the <title> wil;l display, when I hover over the node it should display the <description> and when I click it should send my browser to <link>

Please help.....

thanks




Answer this question

XML in Treeview

  • Ofir Epstein

    the .innertext gives me either a new instance is required error or it gives me all of the inner text for each child node thereafter. Close thanks Im on the right track but not there. I am on a time crunch if you can help more please do.......

  • Trochon

    A few minor adjuctments and it flew thanks very much......

  • GaryMcC

    Ryan I am sorry if I seem abrupt its a little hard for me with this company. They are new to .net and so am I. Im coming from a VB6 background but I am trying...... Anyway here is my XML structure....

    < xml version="1.0" >

    <index>
    <titles id = "Title">
    <title id = "Herbs">
    <link id = "url">C:\Documents and Settings\HP_Owner\My Documents\Dominion\Content\Database\herbs.htm</link>
    <description> a book on herbs</description>
    </title>
    <title id = "Herbs1">
    <link id = "url">C:\Documents and Settings\HP_Owner\My Documents\Dominion\Content\Database\herbs1.htm</link>
    <description> another book on herbs</description>
    </title>
    </titles>
    </index>

    the links will be either on the users harddrive or on a network drive, but that shouldnt matter.... The description should appear in the tooltip section. I really thanks for your help.... BTW in that last section you gave me it kept saying that the innertext was a null refrence. I have tried to use others source but that what it keeps giving me even on prepackaged source with prepackaged XML... no idea why....

    thanks again for your effort



  • Amanda Jamin

    The title is the "id" attribute of the "title" element

    The link element and decription elements are the child elements of title, here's some more code you can try:

    Imports System.Xml

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    TreeView1.ShowNodeToolTips = True
    Dim doc As New XmlDocument

    'change the path to the actual location of your xml document
    doc.Load("C:\titles.xml")

    Dim tnode As TreeNode

    For Each node As XmlNode In doc.SelectNodes("//title")
    Dim booktitle As XmlAttribute = node.Attributes("id")
    Dim linkNode As XmlNode = node.SelectSingleNode("descendant::link")
    Dim descNode As XmlNode = node.SelectSingleNode("descendant::description")

    If Not booktitle Is Nothing Then
    tnode = TreeView1.Nodes.Add(node.Attributes("id").InnerText)
    Else
    tnode = TreeView1.Nodes.Add("Unknown book")
    End If

    If Not linkNode Is Nothing Then
    tnode.Tag = linkNode.InnerText
    End If

    If Not descNode Is Nothing Then
    tnode.ToolTipText = descNode.InnerText
    Else
    tnode.ToolTipText = "No description"
    End If

    Next
    End Sub

    Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
    If e.Node.Tag IsNot Nothing Then
    Process.Start(e.Node.Tag)
    End If
    End Sub

    End Class

    But as I state before in order to correctly process an XML files you will need to know it's structure, and map the relationships of the elements to your code. Here's a link on MSDN library you may find useful:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconemployingxmlinnetframework.asp



  • Altug Atik

    Can't help much unless I know the structure of your XML file

    You'll have adjust the Xpath (the "//title" part) expression, and get the values for each node accordingly.



  • DavidAWinter

    Make a new class and then create an instance of the class that contains the items in your XML doc .

    Your class properties could be "title", "link", "description". or something of the sort.

    There is a XML tool that converts an XML document to a class, look here in msdn for the tool "xsd.exe" I used it with great sucess for a XML doc that had many child nodes that I needed to use as properties of a custom object. You have to use the command line to use it but it sure saved me time, I didn't have to code the class module at all. xsd.exe did it for me! Once I created a new instance of my class it was easy to acess the properties, which would assist you in setting the properties of your tree view.

    Use a collection to iterate through all your other "books" in the collection and add them to your tree view.


  • Carolina Gomes

    Hi,

    In order to process XML files you need to be sure of its structure, suppose your complete xml document looks like this:

    <books>

    <title>Herbs</title>
    <link>C:\Documents and Settings\HP_Owner\My Documents\Dominion\Content\Database\herbs.html</link>
    <description>a book on herbs</description>

    <title>Book2</title>
    <link>c:\books\books2.html</link>
    <description>second book</description>

    <!-- and so on -->

    </book>

    The relationship between the elements is that for every "title" element, it has 2 siblings(elements on the same level), it's next sibling is the link, and the one after is the description.

    Here's some sample code for you to play with, create a new form and drop a treeview on it:

    Imports System.Xml

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    TreeView1.ShowNodeToolTips = True
    Dim doc As New XmlDocument

    'change the path to the actual location of your xml document
    doc.Load("C:\books.xml")

    Dim tnode As TreeNode

    For Each node As XmlNode In doc.SelectNodes("//title")
    tnode = TreeView1.Nodes.Add(node.InnerText)
    tnode.Tag = node.NextSibling.InnerText
    tnode.ToolTipText = node.NextSibling.NextSibling.InnerText
    Next
    End Sub

    Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
    If e.Node.Tag IsNot Nothing Then
    Process.Start(e.Node.Tag)
    End If
    End Sub

    End Class



  • __murph__

    I got that thats great but I am not at all knowledgable in this perticular thing XML and VB.net I have me class done I am having a problem setting and accessing the properties

     I am new at .NET as well as XML and I am trying to make my selections  navigate to a node that does not show in the treeview. The second child of the XML doc please give a good detailed information for me to help me understand.....

     



  • XML in Treeview