Creating a Dataset File with ADO.NET Methods
The last way to create a dataset file is to use the methods of the ADO.NET classes. The ADO.NET namespace is designed to make working with XML files almost effortless. It converts XML schema to and from the internal representation. Use the WriteXMLSchema() method to save the schema to a file. Simply pass it the filename to save it to.
First create a dataset object that links to the data you want to report on. Since the dataset object can be used to connect to a wide variety of data sources, this gives you great flexibility for what your report prints. Once the dataset is created, call the WriteXMLSchema() method and pass it the filename. This saves the XML schema to the file to be used by your report.
The code in listing 17-2 gives you a simple example of how to implement this. It takes a filename and a DataSet object and creates an XML schema file. Make sure that the DataSet object has been populated elsewhere in your application before calling this procedure. Also make sure that the filename has a .ds extension.
'Write a populated dataset object to an XML schema file
'The XmlFileName should have a ".ds" file extension
Public Sub CreateXmlFile(ByVal XmlFileName As String, ByVal XmlDataset As DataSet)
XmlDataset.WriteXmlSchema(XmlFileName)
End Sub
The code in this listing is very generic and can be used anywhere in your application. It is meant to be executed once prior to creating the report. It doesn’t have to be part of your final application and you can comment it out.