Solved

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

Posted on 2014-02-24
10
202 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now