Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-02-24
10
Medium Priority
?
218 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Introduction to Processes
Screencast - Getting to Know the Pipeline

885 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