Recipe 13.9. Writing In-Memory Data Tables to an XML File


Problem

You have some data in a DataSet object, and you would like to export it to an XML file for later reimportation.

Solution

Use the DataSet's WriteXML() method to send the DataSet content to the file in a common XML format.

Discussion

Recipe 13.8 builds a DataTable object with two state-specific records. The following code adds that table to a DataSet object and writes its records to an XML file:

 Dim fullDataSet As New Data.DataSet fullDataSet.Tables.Add(stateTable) fullDataSet.WriteXml("C:\StateInfo.xml") 

These statements generate the following XML content:

 <?xml version="1.0" standalone="yes"?> <NewDataSet>   <UnitedStates>     <ShortName>WA</ShortName>     <FullName>Washington</FullName>     <Admission>1889-11-11T00:00:00-08:00</Admission>     <Population>5894121</Population>   </UnitedStates>   <UnitedStates>     <ShortName>MT</ShortName>     <FullName>Montana</FullName>     <Admission>1889-11-08T00:00:00-08:00</Admission>     <Population>902195</Population>   </UnitedStates> </NewDataSet> 

You can also output the XML directly from the DataTable object without using a DataSet object:

 stateTable.WriteXML("C:\StateInfo.xml") 

ADO.NET was designed with an understanding of data from an XML perspective. Publicly, it exposes this awareness through several XML-specific methods, including the WriteXML() method. The schema generated with this XML database is crafted for efficient processing by ADO.NET. When you later import the exported data from the XML file to a DataSet or DataTable object, ADO.NET will complain if the data doesn't match a format it understands.

To access the schema that matches the exported data, use the related WriteXMLSchema() method:

 stateTable.WriteXMLSchema"C:\StateSchema.xml") 

See Also

Recipe 13.10 shows you how to bring the exported data back into a DataSet object.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net