Solved

Add items to 3 separate listview colums when readuing through xml nodes

Posted on 2014-02-24
10
206 Views
Last Modified: 2014-02-25
Any idea how I get the three separate data pieces into their own column on a listview? They all end up in the very first column.

        For Each sPath As String In IO.Directory.GetFiles(TextBox1.Text)
            TextBox2.Text = sPath
            If IO.Path.GetExtension(sPath) = ".xml" Then

                Dim doc As New XmlDocument()
                Dim nodes As XmlNodeList
                doc.Load(sPath)

                nodes = doc.SelectNodes("/log/entry")
                Dim node As XmlNode

                For Each node In nodes

                        Dim nodecliconnaddr As XmlNode = node.SelectSingleNode("cliconnaddr")
                    If nodecliconnaddr IsNot Nothing Then
                        ListView1.Columns(0).ListView.Items.Add(nodecliconnaddr.InnerText)
                        'ListBox1.Items.Add(nodecliconnaddr.InnerText)
                    End If

                            Dim nodecmd As XmlNode = node.SelectSingleNode("cmd")
                    If nodecmd IsNot Nothing Then
                        ListView1.Columns(1).ListView.Items.Add(nodecmd.InnerText)
                        'ListBox2.Items.Add(nodecmd.InnerText)
                    End If

                            Dim nodelog_time As XmlNode = node.SelectSingleNode("log_time")
                    If nodelog_time IsNot Nothing Then
                        ListView1.Columns(2).ListView.Items.Add(nodelog_time.InnerText)
                        '   ListBox3.Items.Add(nodelog_time.InnerText)
                    End If

                Next
            End If

Open in new window

0
Comment
Question by:MarcViste
  • 5
  • 5
10 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39883930
Change line 16 to

ListView1.Columns(0).SubitemsItems.Add(nodecliconnaddr.InnerText)

Line 22 to

ListView1.Columns(0).SubItems.Add(nodecmd.InnerText)

Line 28 to

ListView1.Columns(0).SubItems.Add(nodelog_time.InnerText)



*Untested code
0
 

Author Comment

by:MarcViste
ID: 39883950
I get the following error for all three lines the first one had items twice as well but I sqaw and fixed that right away)

'Subitems' is not a member of 'system.windows.forms.ColumnHeader'
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39884044
Ah sorry try

ListView1.Items(0).SubitemsItems.Add(nodecliconnaddr.InnerText)
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:MarcViste
ID: 39884059
at least the command is on the right track, when I run it I get the following:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll

Additional information: InvalidArgument=Value of '0' is not valid for 'index'.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39884082
Try 1
0
 

Author Comment

by:MarcViste
ID: 39884096
tried 1, 2, 3     ...
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39885359
I got around to testing the code.

Here is your updated code


        For Each sPath As String In IO.Directory.GetFiles(TextBox1.Text)
            TextBox2.Text = sPath
            If IO.Path.GetExtension(sPath) = ".xml" Then

                Dim doc As New XmlDocument()
                Dim nodes As XmlNodeList
                doc.Load(sPath)

                nodes = doc.SelectNodes("/log/entry")
                Dim node As XmlNode

                For Each node In nodes
                        Dim item As New ListViewItem("Text in first column")
                        Dim nodecliconnaddr As XmlNode = node.SelectSingleNode("cliconnaddr")
                    If nodecliconnaddr IsNot Nothing Then
                        item.SubItems.Add(nodecliconnaddr.InnerText)
                    End If

                            Dim nodecmd As XmlNode = node.SelectSingleNode("cmd")
                    If nodecmd IsNot Nothing Then
                        item.SubItems.Add(nodecmd.InnerText)
                    End If

                            Dim nodelog_time As XmlNode = node.SelectSingleNode("log_time")
                    If nodelog_time IsNot Nothing Then
                        item.SubItems.Add(nodelog_time.InnerText)
                    End If
                    ListView1.Items.Add(item)
                Next
            End If

Open in new window

0
 

Author Comment

by:MarcViste
ID: 39885598
So this code displays "Text in first column" in the first column and then adds the ccliconnaddr.inner text properly in the second, and then lumps the 2nd and 3rd info fields together in the third column.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39885720
Try following then


        For Each sPath As String In IO.Directory.GetFiles(TextBox1.Text)
            TextBox2.Text = sPath
            If IO.Path.GetExtension(sPath) = ".xml" Then

                Dim doc As New XmlDocument()
                Dim nodes As XmlNodeList
                doc.Load(sPath)

                nodes = doc.SelectNodes("/log/entry")
                Dim node As XmlNode

                For Each node In nodes
                        Dim item As New ListViewItem("Text in first column")
                        Dim nodecliconnaddr As XmlNode = node.SelectSingleNode("cliconnaddr")
                    If nodecliconnaddr IsNot Nothing Then
                        item.Text = (nodecliconnaddr.InnerText)
                    End If

                            Dim nodecmd As XmlNode = node.SelectSingleNode("cmd")
                    If nodecmd IsNot Nothing Then
                        item.SubItems.Add(nodecmd.InnerText)
                    End If

                            Dim nodelog_time As XmlNode = node.SelectSingleNode("log_time")
                    If nodelog_time IsNot Nothing Then
                        item.SubItems.Add(nodelog_time.InnerText)
                    End If
                    ListView1.Items.Add(item)
                Next
            End If

Open in new window

0
 

Author Closing Comment

by:MarcViste
ID: 39885731
Still some tiny bugs to work out, but this got me on the right track! Thank you!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

777 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