Adding data to listview from xml to two columns

RoyLittle0

Active member
Joined
Nov 17, 2012
Messages
38
Location
Derby, Uk
Programming Experience
Beginner
How can i add data to a listview from a xml file, i want to add two columns to the list view, i can add data to a listbox but i cant seem to do the same to a listview, the data i need to add is from a xml file, which is "id" and "Name" in two separate columns, they are part of a cascading set of 12 listview boxes each dependent on the one above and i need the id to implement the next cascade.

There are 12 list views in total, so i assume i need to define what goes into each of the columns, can this be done globally for all listviews?

The below code has been modified for listboxes to give me the additional column but i cant seem to get it to work in listview

any pointers would be appreciated

VB.NET:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBoxes.AddRange({Solution1, Solution2, Solution3, Solution4, Solution5, Solution6, Solution7, Solution8, Solution9, Solution10, Solution11, Solution12}) 'this has to be done here to ensure that the controls have initialised


        doc = XDocument.Load(IO.Path.Combine(Application.StartupPath, "Test.xml"))
        ' This will fill solution box 1 with the Root 1 information from the xml file
        Dim Root As New ListViewItem
            (
        From A In doc...<Section1>
        Select Section1 = A.@id()
            ).ToArray


        Solution1.Items.Add(Root)


    End Sub


    Private Sub Sloution1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


        Select Case ListBoxes.IndexOf(sender)
            'Solution1.click event, fills Solution box 2 with values based on the information in Solution box 1
            Case 0
                Dim Root2 =
                (
                    From a In doc...<Section1>
                    Where a.@id = Solution1.Text
                    From b In a.<Root2>.<Section2>
                    Select b.@id
                ).ToArray


                Solution2.Items.Add(Root2)
                'Solution2.click event, fills Solution box 3 with values based on the information in Solution box 2
            Case 1
                If Solution2.Items.Count > 0 Then
                    Dim Root3 =
                    (
                    From b In doc...<Section2>
                    Where b.@id = (Solution2.Text)
                    From c In b.<Root3>.<Section3>
                    Select c.@id
                    ).ToArray


                    Solution3.Items.Add(Root3)
                End If
                'Solution3.click event, fills Solution box 4 with values based on the information in Solution box 3
            Case 2
                If Solution3.Items.Count > 0 Then
                    Dim Root4 =
                    (
                    From c In doc...<Section3>
                    Where c.@id = Solution3.Text
                    From d In c.<Root4>.<Section4>
                    Select d.@id
                    ).ToArray


                    Solution4.Items.Add(Root4)
                End If
                'Solution4.click event, fills Solution box 5 with values based on the information in Solution box 4
            Case 3
                If Solution4.Items.Count > 0 Then
                    Dim Root5 =
                    (
                    From d In doc...<Section4>
                    Where d.@id = Solution4.Text
                    From f In d.<Root5>.<Section5>
                    Select f.@id
                    ).ToArray


                    Solution5.Items.Add(Root5)
                End If
                'Solution5.click event, fills Solution box 6 with values based on the information in Solution box 5
            Case 4
                If Solution5.Items.Count > 0 Then
                    Dim Root6 =
                    (
                    From f In doc...<Section5>
                    Where f.@id = Solution5.Text
                    From g In f.<Root6>.<Section6>
                    Select g.@id
                    ).ToArray


                    Solution6.Items.Add(Root6)
                End If
                'Solution6.click event, fills Solution box 7 with values based on the information in Solution box 6
            Case 5
                If Solution6.Items.Count > 0 Then
                    Dim Root7 =
                    (
                    From g In doc...<Section6>
                    Where g.@id = Solution6.Text
                    From h In g.<Root7>.<Section7>
                    Select h.@id
                    ).ToArray


                    Solution7.Items.Add(Root7)
                End If
                'Solution7.click event, fills Solution box 8 with values based on the information in Solution box 7
            Case 6
                If Solution7.Items.Count > 0 Then
                    Dim Root8 =
                    (
                    From h In doc...<Section7>
                    Where h.@id = Solution7.Text
                    From i In h.<Root8>.<Section8>
                    Select i.@id
                    ).ToArray


                    Solution8.Items.Add(Root8)
                End If
                'Solution8.click event, fills Solution box 9 with values based on the information in Solution box 8
            Case 7
                If Solution8.Items.Count > 0 Then
                    Dim Root9 =
                    (
                    From i In doc...<Section8>
                    Where i.@id = Solution8.Text
                    From j In i.<Root9>.<Section9>
                    Select j.@id
                    ).ToArray


                    Solution9.Items.Add(Root9)
                End If
                'Solution9.click event, fills Solution box 10 with values based on the information in Solution box 9
            Case 8
                If Solution9.Items.Count > 0 Then
                    Dim Root10 =
                    (
                    From j In doc...<Section9>
                    Where j.@id = Solution9.Text
                    From k In j.<Root10>.<Section10>
                    Select k.@id
                    ).ToArray


                    Solution10.Items.Add(Root10)
                End If
                'Solution10.click event, fills Solution box 11 with values based on the information in Solution box 10
            Case 9
                If Solution10.Items.Count > 0 Then
                    Dim Root11 =
                    (
                    From k In doc...<Section10>
                    Where k.@id = Solution10.Text
                    From l In k.<Root11>.<Section11>
                    Select l.@id
                    ).ToArray


                    Solution11.Items.Add(Root11)
                End If
                'Solution11.click event, fills Solution box 12 with values based on the information in Solution box 11
            Case 10
                If Solution11.Items.Count > 0 Then
                    Dim Root12 =
                    (
                    From l In doc...<Section11>
                    Where l.@id = Solution11.Text
                    From m In l.<Root12>.<Section12>
                    Select m.@id
                    ).ToArray


                    Solution12.Items.Add(Root12)
                End If


        End Select
    End Sub
 
Last edited:
OK, i have 1 xml file that is loaded into form 1, it is in a Public Sub and i wont to be able to use this in my form 6 that i will be using as a xml modify/save form, how do i link the two forms so that i do not have to open the xml twice.

Form 1
VB.NET:
Option Strict On
Imports System.Xml
Imports System.IO


Public Class Form1
    Dim xmlDoc As New XmlDocument()


    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strmReader As New StreamReader("C:\VisionProject\IBS2000.xml", System.Text.Encoding.UTF8)
        Dim xmlReader As XmlTextReader = New XmlTextReader(strmReader)
        Me.AutoScroll = True
        Me.VScroll = True
        Me.HScroll = True
        'read the file and load the first list view
        xmlReader.WhitespaceHandling = WhitespaceHandling.None
        xmlDoc.Load(xmlReader)


        Dim strXMLPos As String = "//Root/Root1/Section1"
        Dim xml_LV1List As XmlNodeList = xmlDoc.SelectNodes(strXMLPos)
        For Each curNode As XmlNode In xml_LV1List
            Dim LVI As New ListViewItem
            With LVI
                .Text = curNode.Attributes.ItemOf("Name").InnerText
                .SubItems.Add(curNode.Attributes.ItemOf("id").InnerText)
                .SubItems.Add(curNode.Attributes.ItemOf("Label").InnerText)
            End With
            Listview_1.Items.Add(LVI)
        Next
        'add each list view header
        AddListViewHeader(Listview_1)
        AddListViewHeader(ListView_2)
        AddListViewHeader(Listview_3)
        AddListViewHeader(Listview_4)
        AddListViewHeader(Listview_5)
        AddListViewHeader(Listview_6)
        AddListViewHeader(Listview_7)
        AddListViewHeader(Listview_8)
        AddListViewHeader(Listview_9)
        AddListViewHeader(Listview_10)
        AddListViewHeader(Listview_11)
        AddListViewHeader(Listview_12)
    End Sub


    'this add's the list view headers
    Private Sub AddListViewHeader(ByVal LV As ListView)
        Dim Headers() As ColumnHeader = {New ColumnHeader With {.Text = "Name", .Width = 200},
                                         New ColumnHeader With {.Text = "ID", .Width = 90, .TextAlign = HorizontalAlignment.Center},
                                         New ColumnHeader With {.Text = "Label", .Width = 2000}}
        LV.Columns.Clear()
        LV.Columns.AddRange(Headers)
    End Sub

Form 6
VB.NET:
Imports System.Xml


Public Class Form6


    Public Sub New()
        MyBase.New()
        InitializeComponent()
        BuildTree(TreeView1, XDocument.Load("C:\VisionProject\IBS2000.xml"))
    End Sub


    Private Sub BuildTree(ByVal treeView As TreeView, ByVal doc As XDocument)
        Dim treeNode As TreeNode = New TreeNode(doc.Root.Name.LocalName)
        treeView.Nodes.Add(treeNode)
        BuildNodes(treeNode, doc.Root)
    End Sub


    Private Sub BuildNodes(ByVal treeNode As TreeNode, ByVal element As XElement)
        For Each child As XNode In element.Nodes
            Select Case (child.NodeType)
                Case XmlNodeType.Element
                    Dim childElement As XElement = CType(child, XElement)
                    Dim childTreeNode As TreeNode = New TreeNode(childElement.Name.LocalName)
                    treeNode.Nodes.Add(childTreeNode)
                    BuildNodes(childTreeNode, childElement)
                Case XmlNodeType.Text
                    Dim childText As XText = CType(child, XText)
                    treeNode.Nodes.Add(New TreeNode(childText.Value))
            End Select
        Next
    End Sub
    Private Sub GetAttributes(ByVal node As XmlNode, ByVal treeNode As TreeNode)
        If node.Attributes IsNot Nothing Then
            Dim attributes As ListViewItem() = New ListViewItem(node.Attributes.Count - 1) {}


            For i As Integer = 0 To node.Attributes.Count - 1
                attributes(i) = New ListViewItem(New String() {node.Attributes(i).Name, node.Attributes(i).Value})
            Next


            treeNode.Tag = attributes
        End If
    End Sub


    Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
        AttributeListView.Items.Clear()


        If e.Node.Tag IsNot Nothing Then
            AttributeListView.Items.AddRange(DirectCast(e.Node.Tag, ListViewItem()))
        End If
    End Sub




    Private Sub AttributesListView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AttributeListView.Click
        With Me.AttributeListView
            Dim i As Integer
            For Each item As ListViewItem In AttributeListView.SelectedItems
                i = item.Index
            Next


            Dim innercounter As Integer = 0
            For Each subItem As ListViewItem.ListViewSubItem In AttributeListView.Items(i).SubItems
                Dim myString As String = AttributeListView.Items(i).SubItems(innercounter).Text
                Select Case innercounter
                    Case 1


                End Select
                innercounter += 1
            Next
        End With
    End Sub


    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect


    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


    End Sub
End Class
 
Hi Roy,

This is where you need to consider the SCOPE of your "xmlDoc" variable. Currently the scope of this variable is Private to Form1 and so is not accessible outside the scope of the form. To combat this you need to declare the variable with Public scope.

This can be done in two main ways:-

1) You can set the scope of the variable to Public in its current context i.e:- Public xmlDoc As XMLDocument within the current Form1. This can then be accessed outside the form using Form1.xmlDoc.

2) You can add a Code Module to the Project and then declare the variable in the module i.e:- Public xmlDoc As XMLDocument. Doing it this way you can then access the xmlDoc variable anywhere in your project.

Hope that helps.

Cheers,

Ian
 
Thanks Ian

I have set a module in Form 1 for my xmlDoc so that i can use it in any form if needed, what method do i use to call this to load my treeview in another form, i have also set a Public Module in form 6, i have looked on the internet but cant seem to find much info on this.

Just bought two books to read:-
Beginning VB.Net XML
XML Black Book
Is there a good one for VB.NET? i looked at the VB.NET for Dummies but wasn't impressed

VB.NET:
Option Strict On
Imports System.Xml
Imports System.IO


Public Module CoreModule
    Public xmlDoc As New XmlDocument()
End Module


Public Class Form1

VB.NET:
Imports System.Xml
Imports System.IO


Public Class Form6
    Public NotInheritable Class CoreModule


    End Class


    Private Sub BuildTree(ByVal treeView As TreeView, ByVal doc As XDocument)
        Dim treeNode As TreeNode = New TreeNode(doc.Root.Name.LocalName)
        treeView.Nodes.Add(treeNode)
        BuildNodes(treeNode, doc.Root)
    End Sub
 
Hi Roy,

A few things for you here:-

1) There is actually nothing wrong with declaring a module the way you have but it is "dirty", for want of a better word, to create it within a form declaration. What you should do is add a new module from your Solution Explorer which then adds a new file to your project which then keeps your Public declarations in one neat and tidy place.

2) You can now use the variable xmlDoc in any code block of any form as and when you wish. I notice in your BuildTree sub you pass a parameter Doc as XDocument. This is now not necessary since you can just use the xmlDoc variable as needed.

3) You say:-

I have also set a Public Module in form 6
You have not. You have actually created a new class which you just happen to call CoreModule. I am not sure what you trying to achieve here but it does not look as though it is necessary.

4) There are loads of VB.NET books available. Just have a browse round the net. For starters have a look at this online tutorial for beginners:-

Microsoft Visual Basic .NET tutorials for Beginners

Hope that helps.

Cheers,

Ian
 
Thanks Ian,

In all the searching on the net i have never come across that link before, i'll work through it over the weekend

Ok, it makes a little more sence now

Module
VB.NET:
Imports System.Xml


Module Module1
    Public xmlDoc As New XmlDocument()
End Module

Form 1
VB.NET:
Option Strict On
Imports System.Xml
Imports System.IO


Public Class Form1


    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strmReader As New StreamReader("C:\VisionProject\IBS2000.xml", System.Text.Encoding.UTF8)
        Dim xmlReader As XmlTextReader = New XmlTextReader(strmReader)
        Me.AutoScroll = True
        Me.VScroll = True
        Me.HScroll = True
        'read the file and load the first list view
        xmlReader.WhitespaceHandling = WhitespaceHandling.None
        xmlDoc.Load(xmlReader)
Form 6
VB.NET:
Imports System.Xml
Imports System.IO


Public Class Form6


    Private Sub BuildTree(ByVal treeView As TreeView, ByRef xmlDoc........................)
        Dim treeNode As TreeNode = New TreeNode(xmlDoc.Root.Name.LocalName)
        treeView.Nodes.Add(treeNode)
        BuildNodes(treeNode, xmlDoc.Root)
    End Sub


    Private Sub BuildNodes(ByVal treeNode As TreeNode, ByVal element As XElement)
        For Each child As XNode In element.Nodes
            Select Case (child.NodeType)
                Case XmlNodeType.Element
                    Dim childElement As XElement = CType(child, XElement)
                    Dim childTreeNode As TreeNode = New TreeNode(childElement.Name.LocalName)
                    treeNode.Nodes.Add(childTreeNode)
                    BuildNodes(childTreeNode, childElement)
                Case XmlNodeType.Text
                    Dim childText As XText = CType(child, XText)
                    treeNode.Nodes.Add(New TreeNode(childText.Value))
            End Select
        Next
    End Sub
 

Latest posts

Back
Top