I think the simplest way (although you should ask around) is to first load the XML file into a dataset, and then have some code to stick the dataset's table data into a listview. Listview all by itself does not support reading XML data, but I fill ListViews from datasets all the time and it seems to work better than trying to teach ListView how to understand XML.
For info on datasets and how to read XML files into them:
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemDataDataSetClassTopic.htm
ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconloadingdatasetfromxml.htm
Note that if you aren't using a schema file, the DataSet.ReadXML method can infer the schema from the data if it isn't too complicated:
ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconinferringdatasetrelationalstructurefromxml.htm
Once you have your data loaded into the dataset, you can examine a table and create ListViewItems for each record like so:
Code:
Dim lvwColumn As ColumnHeader
Dim itmListItem As ListViewItem
Dim shtCntr As Short
Dim objRow As DataRow
'Empty the listview
lvwItems.Clear()
'Suspend redrawing until we are done adding items
lvwItems.BeginUpdate()
'populate the listview columns
Try
For shtCntr = 0 To dsDataSet.Tables("TableName").Columns.Count - 1
lvwColumn = New ColumnHeader()
'Create Column Header
lvwColumn.Text = dsDataSet.Tables("TableName").Columns.Item(shtCntr).ColumnName.ToString
lvwItems.Columns.Add(lvwColumn)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
'Populate the listview items
For Each objRow In dsDataSet.Tables("TableName").Select("SomeField = 'SomeValue'")
itmListItem = New ListViewItem()
itmListItem.Text = objRow.ItemArray(0)
For shtCntr = 1 To objRow.ItemArray.GetUpperBound(0)
itmListItem.SubItems.Add(objRow.ItemArray.GetValue(shtCntr))
Next
lvwItems.Items.Add(itmListItem)
Next
lvwItems.EndUpdate()
Last edited by Slow_Learner; Apr 23rd, 2003 at 11:30 PM.
Based on:
ms-help://MS.VSCC/MS.MSDNVS/xmlsdk30/htm/xmconintroductiontothedom.htm
...it seems like it would end up being six of one, half dozen of the other. I dunno if either way would be shorter or consume less resources, although for small apps and small data I'm sure it doesn't matter much.
'up near the top, because we want this to be declared app-wide
Private dsData As DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
InitDataSet()
End Sub
Private Sub InitDataSet()
dsData = New DataSet()
Dim myDataColumn As DataColumn
Dim myDataRow As DataRow
Dim iCount As Integer
'Set up table
Dim tbl As DataTable = New DataTable("Number")
tbl.CaseSensitive = True
myDataColumn = New DataColumn()
myDataColumn.DataType = System.Type.GetType("System.Int32")
myDataColumn.ColumnName = "Number"
myDataColumn.Unique = False
' Add the Column to the DataColumnCollection.
tbl.Columns.Add(myDataColumn)
dsData.Tables.Add(tbl)
'Add some records
For iCount = 1 To 100
myDataRow = dsData.Tables("Number").NewRow()
myDataRow("Number") = iCount
dsData.Tables("Number").Rows.Add(myDataRow)
Next
End Sub
As to why you're getting the type not defined errors, you shouldn't be, System.Data is the namespace in which they reside and 'should' be imported by default for a Windows Forms. If the above code gives you the same error, try adding:
'Up at the top of your class you would declare your DataSet
'so it will be accessible application-wide
Dim dsData As New DataSet("Files")
Dim xmlFileName As String = "C:\Documents and Settings\Kenny\My Documents\Visual Studio Projects\Pic Viewer XP\Config.xml"
'Read in the xml file and infer the dataset schema
Private Sub ReadXMLFile()
' Read the XML document back in.
' Create new FileStream to read schema with.
Dim fsReadXml As New System.IO.FileStream _
(xmlFilename, System.IO.FileMode.Open)
' Create an XmlTextReader to read the file.
Dim myXmlReader As New System.Xml.XmlTextReader(fsReadXml)
' Read the XML document into the DataSet.
newDataSet.ReadXml(myXmlReader)
' Close the XmlTextReader
myXmlReader.Close
End Sub
For me at least, this code takes your xml file given above and imports it into a DataSet, one of whose tables is called "Main". This table has one row, which follows the structure you show in your XML file. Now to stick that into a listview:
Code:
Private Sub FillListView()
Dim lvwColumn As ColumnHeader
Dim itmListItem As ListViewItem
Dim shtCntr As Short
Dim objRow As DataRow
'Empty the listview
lvwItems.Clear()
'Suspend redrawing until we are done adding items
lvwItems.BeginUpdate()
'populate the listview columns
Try
For shtCntr = 0 To dsData.Tables("Main").Columns.Count - 1
lvwColumn = New ColumnHeader()
'Create Column Header
lvwColumn.Text = dsData.Tables("Main").Columns.Item(shtCntr).ColumnName.ToString
lvwItems.Columns.Add(lvwColumn)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
'Populate the listview items
'Since we have only one record, let's just select all
'rather than specify a criteria
For Each objRow In dsData.Tables("Main").Select()
itmListItem = New ListViewItem()
itmListItem.Text = objRow.ItemArray(0)
For shtCntr = 1 To objRow.ItemArray.GetUpperBound(0)
itmListItem.SubItems.Add(objRow.ItemArray.GetValue(shtCntr))
Next
lvwItems.Items.Add(itmListItem)
Next
lvwItems.EndUpdate()
End Sub
More rows in your XML should equate to more rows in your listview. Does it not work? Give a copy of your xml file and I'll try it, it should work for any number of rows.
I know its been a while since my last reply. I wanted to know how would I write to an Xml file like this one if wanted to add a test3.kxk file at the end.