By far the easiest way is to use Visual Studios dataset designer.
In the project menu, add new item, you select to add a dataset. You can then add the tables to your dataset as you need them and it'll set up all your objects for you.
You don't really need to know anything about XML. It just writes your dataset out as XML and lets you read the data back into the dataset to use. You never even have to look at the contents of the xml file.
As a simple example of how easy it is...
1. Add a dataset to your project (Project -> New Item -> Dataset, I've called the dataset MyDataset)
2. Add a datatable to the dataset (Right click the dataset designer and click add, then Datatable. I've then renamed the datatable to MyDataTable)
3. Add a column to the datatable (Right click the datatable and click add, then column. I've then renamed the column to MyColumn).
So, I've got a dataset called MyDataSet. That dataset contains a table called MyDatatable. That table contains one column called MyColumn. VB will have created loads of code to make using this quite simple.
Now, looking at using it. In the form for the project, add a textbox, a button and a datagridview. The button will add the textbox contents to the datatable (in the dataset). The datagridview will display the contents of the datatable.
In the form we want to have this Dataset we've created to use...
Code:
Public Class Form1
' Note how this is declared as the dataset that was created
Dim ds As New MyDataSet
End Class
When the form loads it is going to attempt to read any data that has been saved. This assumes that there's a Temp folder in the root of C:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Check if the file exists
If IO.File.Exists("C:\Temp\data.xml") Then
Try
' If the file does exist then attempt to read the file into our dataset object
ds.ReadXml("C:\Temp\data.xml")
Catch ex As Exception
MessageBox.Show(ex.Message, "Error reading Xml")
End Try
End If
' Set up the datagridview to display the data
' Note how the Table is the table that we created in the dataset designer. It's all
' properly typed.
DataGridView1.DataSource = ds.MyDataTable
End Sub
When the button is clicked it's going to add the textbox contents to the datatable
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Check that there's something in textbox1 and quit if not.
If String.IsNullOrEmpty(TextBox1.Text.Trim) Then Exit Sub
' Declare a new row to add. Note how the type is MyDataTableRow. Again, it's properly types.
Dim dr As MyDataSet.MyDataTableRow
' Create the new row from the dataset's table
dr = ds.MyDataTable.NewRow
' Add the texttbox contents. Again, note how the row has the MyColumn property
' that was added to the datatable in the designer.
dr.MyColumn = TextBox1.Text
' Add the row to the datatable. It will automatically appear in the datagridview
ds.MyDataTable.Rows.Add(dr)
End Sub
And finally, when the form closes it will write the data ready for it to be read when it's next loaded.
Code:
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Try
' Tell the dataset to write its data as XML
ds.WriteXml("C:\Temp\data.xml")
Catch ex As Exception
MessageBox.Show(ex.Message, "Error writing Xml")
End Try
End Sub
And that's it. It's all rather tidy.
You can loop over the contents of the data via...
Code:
Dim dr As MyDataSet.MyDataTableRow
For each dr in ds.MyDataTable.Rows
debug.print dr.MyColumn
Next
You can query data out of the datatable like it's a database...
Code:
Dim drs() as MyDataSet.MyDataTableRow
Dim dr as MyDataSet.MyDataTableRow
drs = ds.MyDataTable.Select("MyColumn = 'Bob')
For each dr in drs
debug.print(dr.MyColumn) ' Obviously they're all going to be Bob
Next
Yum.