Click here to Skip to main content
15,849,494 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I'm trying to understand how to insert an XML file into a TreeView, not only the nodes with the texts but also the attributes. In the example XML file, I can display with my code (take from Web) only "Actor" and "Type", while instead I would also like to read "Movie Title = ". How could I do it?

XML
<?xml version="1.0"?>
-<BLURays>
-<Movie Title="Fallen">
<Actor>Denzel Washington</Actor>
<Type>Thriller</Type>
</Movie>

-<Movie Title="Mad Max">
<Actor>Mel Gibson</Actor>
<Type>Sci-Fi</Type>
</Movie>

-<Movie Title="Event Horizon">
<Actor>Sam Neill</Actor>
<Type>Sci-Fi Horror</Type>
</Movie>

-<Movie Title="Happy Gilmore">
<Actor>Adam Sandler</Actor>
<Type>Comedy</Type>
</Movie>

-<Movie Title="The Postman">
<Actor>Kevin Costner</Actor>
<Type>Adventure</Type>
</Movie>
</BLURays>


What I have tried:

VB
Private Sub Prova2ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles Prova2ToolStripMenuItem.Click
    OpenFileDialog1.Multiselect = False
    OpenFileDialog1.ShowDialog()
    Dim fileName As String = Path.GetFileName(OpenFileDialog1.FileName)
    Dim filePath As String = OpenFileDialog1.FileName
    Me.Text = filePath

    Dim xmldoc As New XmlDataDocument()
    Dim xmlnode As XmlNode
    Dim fs As New FileStream(filePath, FileMode.Open, FileAccess.Read)
    xmldoc.Load(fs)
    xmlnode = xmldoc.ChildNodes(1)
    TreeView1.Nodes.Clear()
    TreeView1.Nodes.Add(New TreeNode(xmldoc.DocumentElement.Name))
    Dim tNode As TreeNode
    tNode = TreeView1.Nodes(0)
    AddNode(xmlnode, tNode)

End Sub


Private Sub AddNode(ByVal inXmlNode As XmlNode, ByVal inTreeNode As TreeNode)
    Dim xNode As XmlNode
    Dim tNode As TreeNode
    Dim nodeList As XmlNodeList
    Dim i As Integer
    If inXmlNode.HasChildNodes Then

        nodeList = inXmlNode.ChildNodes
        For i = 0 To nodeList.Count - 1
            xNode = inXmlNode.ChildNodes(i)
            inTreeNode.Nodes.Add(New TreeNode("Node: " + xNode.Name))
            tNode = inTreeNode.Nodes(i)
            AddNode(xNode, tNode)
        Next
    Else
        inTreeNode.Text = inXmlNode.InnerText.ToString
    End If
End Sub
Posted
Updated 2-Oct-23 4:52am
v2
Comments
Graeme_Grant 26-Sep-23 6:57am    
What's the problem exactly? Is there an error?
Marco Giglio 26-Sep-23 12:01pm    
Meanwhile, forgive my poor English... this listing does not give errors but shows the treeview only with the "text" nodes. However, I would also like to see those with attributes (Movie Title)
Graeme_Grant 26-Sep-23 17:08pm    
It is a child to parent relationship. You add nodes to parent nodes.

1 solution

This is a very common question that you can google: winform how to add children to parent nodes on a treeview - Google Search[^] which gave me this: adding child nodes in treeview [solution] - StackOverflow[^]
C#
treeView.Nodes[0].Nodes.Add(yourChildNode);

Where Nodes[0] would be the "Movie", and the yourChildNode would be for each detail: "Actor" and "Type"
 
Share this answer
 
v2
Comments
Marco Giglio 27-Sep-23 3:23am    
Forgive my English... maybe I can't explain myself well...
I figured out how to add a branch to the tree, but what I can't do is add nodes that have attributes instead of text. In the example file I posted, I can't get the following branches to appear:
<movie title="Fallen">, <movie title="Mad Max">, <movie title="Event Horizon">, and <movie title="The Postman">, but only child nodes that have text (such as example: <actor>Adam Sandler
Graeme_Grant 27-Sep-23 3:36am    
That is what I explained how to do. It's a hierarchical data structure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900