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:
Hi Roy,

Good to hear and I was just about to ask that specific question. Post your full XML file if you get stuck and I will have a look of you like.

Might not be today however since I am about done for the day.

Cheers,

Ian
 
Hi Ian,

I think i have found the problem, in my ID i have used (id ="1-12-16-32") and this doesn't seen to work with the code, so i have changed some of them to simple numbers and so far it seems to have solved the problem, so i think that is what is wrong with my xml.

Roy
 
Hi Ian,

I have added the rest of the ListViews to the form and all works well, i will sit and rename all my nodes, all 1500 of them, thanks for your efforts they are greatly appreciated.

Many Thanks

Roy
 
Hi Roy,

I have just picked up your last post and please tell me that you have NOT started the conversion of your 1500 nodes? I used integer types in my solution since that is what was presented in the XML file. If you have strings then just change the code to use strings instead of integers and it will work just as well.

Hope I caught you in time.

Cheers,

Ian
 
Hi Ian,

No ther was a slight calculation error in my sums there is actually 3071 nodes and they have all been renumbered :)

But i copied the xml into excel and used a macro to rename all the id ="" to include a concecutive unmber in between the "" so all is good, it is a much neater way of doing things than what i had before anyway, the macro i used is attached if anyone else finds it useful

Thanks for everything

Roy

VB.NET:
Sub test()
    Dim a, i As Long, ii As Long, n As Long
    With ActiveSheet.UsedRange
        a = .Value
        With CreateObject("VBScript.RegExp")
            .Pattern = "( id ="")("")"
            For i = 1 To UBound(a, 1)
                For ii = 1 To UBound(a, 2)
                    If .test(a(i, ii)) Then
                        n = n + 1
                        a(i, ii) = .Replace(a(i, ii), "$1" _
                        & Format$(n, "0000") & "$2")
                    End If
                Next
            Next
        End With
        .Value = a
    End With
End Sub
 
I have been wondering about doing a database save form for my xml that i can recall a node by its id and modify the "Name" and "Label" tags then save it, how could this be achieved with the xml document already opened by the program, would it need to be saved as a temporary file until the program is closed for it to save.

My xml is 73751 lines long which is why it crossed my mind.
 
Hi Roy,

You can easily create your own editor for the XML file and then use LINQ as we have already used in the solution to create a pointer to a particular node in the XML file which can then be updated. You can then save the updated file using the XMLDocument.Save method.

To use the XMLDocument.Save method you must first remember to close the XmlTextReader after you have loaded the XML file in the load event.

Rather than using LINQ as described above you could also use the XMLDocument.GetElementById() method to find and use a specific element within the file but I have never used this and after a quick look about on the net you need to modify the XML file to define the ID field before you can use GetElementById. I have no experience with this.

After all the above has been said there may well be a better way to do this using a DataSet but as I had mentioned earlier in the thread I did not figure out how to access the attributes of your nodes using a DataSet based on the way your XML file is structured.

Hope that helps.

Cheers,

Ian
 
Hi Ian,

Thanks for the advice, i didn't want to go down the wrong path at the beginning, if i create a save form for this do i need close the xml reader on leaving the first form, load it into the save form and then close after read, after save when i leave the save form, reopen it again in the first form.

So
Form 1 opens with xml reader, open save form which needs to close form1's reader
Save form opens xml reader, then closes reader once read, modify and save xml.
Save form closes and opens xml reader on form 1

If that makes sense

Roy
 
Last edited:
Hi Roy,

Firstly it is always good practice to close your files after reading them to prevent corruption on disk in case of unexpected failure. In your case once you close the reader the contents of the XML file are still persisted in the XML document for you to use in your project.

The second thing to consider is the SCOPE of your XmlDocument variable. If the scope is private to a form or a routine within, say Form1, then the XmlDocument will not be accessible when opening additional forms and you would need to create a new XmlDocument variable and re-read the XML file to edit it. However, you could make the XmlDocument a Public variable which would then be accessible from anywhere in your project. This would then mean that you could edit and save the original XmlDocument that was used to read the file at the start of the project.

Hope that helps.

Cheers,

Ian
 
Hi Roy,

I had a free hour this morning so I have created an XML editor for your file. I have done this with a TreeView so that I have access to all the nodes in a structured manner. This works great.

What is really interesting however is that it failed on the XMLDocument.Save method with a exception " 'utf8' is not a supported encoding name." What I realised is that the encoding declaration of your XML file is incorrect. You have <?xml version="1.0" encoding="utf8"?> and it should be <?xml version="1.0" encoding="utf-8"?>.

You may need to look at this otherwise you are going to run into problems.

By the way, I am happy to share my solution once you have demonstrated that you have given it a good go yourself.

Cheers.

Ian
 
Hi Ian,

Thanks for that, i was working on something yesterday and came up with the same error, now i know why i got it.

I have been looking at a few different solutions and i am learning, so for me that is the end game, this project is just a means to learn and i'm finding it all interesting, this is just something i am doing in my time in the UK, i'll be flying off somewhere soon so wont get much chance to work on it then.

Roy
 
Back
Top