Solved

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

Posted on 2014-02-24
10
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this post we will learn different types of Android Layout and some basics of an Android App.
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…

739 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