DataSets have a WriteXml method that can write the contents and schema of a DataSet to an XML file. This file can then be used to populate another DataSet so that it contains exactly the same data as the original.
Writing a DataSet to an XML file
The DataSet.WriteXml method has a number of overloads. A full description of each of them can be found
in the MSDN documentation. For our purposes, all we need is the seventh one on that list: DataSet.WriteXml(string, XmlWriteMode).
The first argument to WriteXml is the path to the XML file you'd like to create. This file can be anywhere that your program has write access to. If the file indicated by the path does not exist, it will be created. If it does exist, it will be overwritten. Keep this in mind if you're creating multiple files -- each one should have a unique filename.
The second argument to WriteXml is a member of the XmlWriteMode enumeration that tells it whether or not to include the DataSet's schema, or to write the data as a DiffGram. Since we always want the data to be written along with the schema, this argument should always be XmlWriteMode.WriteSchema. The following code illustrates the process of creating an XML file from a DataSet.
[C#]
|
[VB.NET]
|
|
DataSet ds;
// ... Fill the DataSet with data from your database ...
ds.WriteXml("XML/MyData.xml", XmlWriteMode.WriteSchema);
Dim ds As DataSet
' ... Fill the DataSet with data from your database ...
ds.WriteXml("XML/MyData.xml", XmlWriteMode.WriteSchema)
|
Reading an XML file into a DataSet
Taking this XML file and using it to fill a dataset is just as easy. If you're just sending data to Technical Services then you don't need to worry about this section; once you get us the XML file we'll handle it. Writing and reading XML files may be useful to you in other situations, though, so information on ReadXml is included for the sake of completeness.
DataSets have a ReadXml function that mirrors the WriteXml function. ReadXml takes an XML file and populates a DataSet with the data inside it. Just as above, we'll use the overload of this function that takes a string and a member of the ReadXmlMode enumeration.
The first argument, again, indicates a file. This time the file must exist and contain XML data, and your program must have read permissions.
The second argument tells ReadXml how to read the file. If you're reading an XML file that was created with the code above, set this to XmlReadMode.ReadSchema. Note that if the DataSet already has a schema when you call ReadXml, this may throw an error if the XML schema and the existing schema conflict. Because of this, it is recommended to use XmlReadMode.ReadSchema on a blank DataSet. A full list of the XmlReadMode options and how they treat schemas can be found in the documentation. The following code reads an XML file into a new DataSet.
[C#]
|
[VB.NET]
|
|
DataSet ds = new DataSet();
ds.ReadXml("XML/MyData.xml", XmlReadMode.ReadSchema);
Dim ds As DataSet = new DataSet()
ds.ReadXml("XML/MyData.xml", XmlReadMode.ReadSchema)
|
Note that while ReadXml can accept any valid XML file, it works best when the input file has a structure it can easily read. Because of this, it's easiest to use ReadXml on files that were created with WriteXml.
While this article discussed DataSets, the same technique can be used with other ADO.NET objects. DataTables have ReadXml and WriteXml functions that work just like the ones for DataSets. DataReaders and DataViews do not have these functions, but they can be converted to DataTables and then written to XML. Turning a DataReader into a DataTable is best accomplished through the DataTable.Load method. DataViews have a ToTable method that will return a DataTable with the data as it's been filtered or sorted by the DataView. |