XML and Strongly-Typed Datasets - Part 2
I did a bit of research and found another way to allow design-time usage of an XML file. This one has a few more steps than my previous post, but I think it is more useful. I am going to provide a simple example in this post, but will follow it up with a more complex example. You will need the Visual Basic Power Pack XML to Schema tool installed, which you can download from here - http://msdn.microsoft.com/en-us/vbasic/bb840042.aspx. Hopefully this walkthrough will be helpful.
- Create a new windows project in VB 2008.
- Add a new xml file, call it books.xml. When you see the xml designer you can replace the code with the books.xml code from here - http://msdn.microsoft.com/en-us/library/ms762271.aspx
- Set the Copy To Output Directory to "Copy Always" for this example.
- Now add another new file and select the XML to Schema template.
- I called the file BooksSchema.xsd and then click add. Another screen will pop up, click on Add from File.
- Select the books.xml file you created and click ok.
- Click Ok again.
- Now, you can click on the Data Sources Tab and it will show the catalog dataset. Drag the Books icon onto the form design surface and it will automatically create a datagridview with the columns from the XML file.
- Now go the code behind and add this line of code to the Load event.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Me.Load
Catalog.ReadXml("Books.xml")
End Sub
- In the end it should look like this:
- Now, if you want to view the schema like you do a dataset, you need to right click on the schema and select Open with... and select Dataset Editor from the list. Click OK. A warning will come up, but click yes. Now you will see the XML schema like you do a regular dataset.

Next week, I will use an example where there are relationships in the XML and use that to create multiple datagridviews with a parent-child relationship.