I apologize for how late this post is, but Christmas snuck up on me and I had to rush to meet all the family obligations. In this post I want to look at using more complex XML files which have parent-child relationships as well as how to validate XML files. Another customer of mine wanted code to fill a dataset with data from XML, but also needed to enforce some rules. I used code to validate the document due to a few factors outside my control, but I like to see if there would have been a better way to do this. So now I have the opportunity to replicate the problem and hopefully do a better job solving it. As always input is welcome if you have a better way to do this. Otherwise let us begin.
Setup
Before we being you will need to get a few files from Microsoft. The XML file, CustomerOrders.xml, is located here - http://msdn.microsoft.com/en-us/library/bb387025.aspx. Next, create a new windows forms application, adding the XML file to the project. If you read my last blog post, you know all you need to do is use the XML-to-Schema tool and it will generate the needed xsd file. After that, set the custom tool property of the xsd file to MSDatasetGenerator.
After that right click on the xsd file in the solution explorer and select "Run Custom Tool." A new datasource will now show up in the Data Sources tab. I need to make one change to the dataset since I want to show customers and their orders (the relationship was not automatically made). Right click on the dataset and select "Open With..." From there right click on the design surface and select "Add>Relation..." Set the relationship like the image below:
A one-to-many relationship will be created. you can now drag items from the Data Sources tab onto the form and modify the look during design time. Select the "Copy to Output Directory" property and set it to "Copy Always." I am only doing this for brevity sake, you would be better off to set a configurable location in the app.config file to better handle this.
Create the UI
I created the UI by dragging the Customer table (select Details, not DataGridView) from the Data Sources tab onto the form. This creates the CustomerBindingNavigator as well as the labels and textboxes.

The Customer_Id and Customers_Id fields were removed, since I didn't want to display them. Next, I dragged the Order table that is below the Customer Table in the drop down list and dragged it onto the form. A datagridview will be created for the Order table which includes the code to display the correct orders when the parent customer is selected.
Validation
Double click on the form and add the following code:
Imports System.Xml
Imports System.Xml.Schema
'The validation code is from the MSDN website - http://msdn.microsoft.com/en-us/library/bb387037.aspx
'so all credit belongs to them.
Public Class Form1
Dim errors As Boolean = False 'Holds validation state for the XDocument
'Capture the XSD error event to show what caused the failue
Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
MessageBox.Show(String.Format("{0}", e.Message))
errors = True
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Load the schema
Dim schemas As XmlSchemaSet = New XmlSchemaSet()
schemas.Add("", "CustomersOrders.xsd") 'There is no namespace on the xml, so use blank for the first parameter
'Load the xml
Dim custOrdDoc As XDocument = XDocument.Load("CustomersOrders.xml")
'Initialize the error flag
errors = False
'Validate the xdocument with the schema
custOrdDoc.Validate(schemas, AddressOf XSDErrors)
MessageBox.Show(String.Format("custOrdDoc {0}", IIf(errors, "did not validate", "validated")))
If errors = False Then
'Fill the stronly-typed dataset if successful.
Root.ReadXml(custOrdDoc.CreateReader)
End If
End Sub
End Class
Now when you run program it will display the validation status and then display the results. The UI isn't that exciting, but the ability to create a UI during design time rather than run time as well as validate the loaded XML makes working with XML just that much easier. Combine this with VB 9's ability to use XML literals and LinqToXML, you have some powerful tools to work with XML in .Net.