using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
namespace DataSetSerializer
{
class Program
{
static void Main(string[] args)
{
//Serialize a data table to a string
DataTable dataTable = new DataTable("MerricksTableName");
DataColumn column = new DataColumn("ProductId", typeof(int));
column.AutoIncrement = true;
dataTable.Columns.Add(column);
column = new DataColumn("Product description", typeof(string));
column.Expression = "ProductId + ProductId";
dataTable.Columns.Add(column);
// Add ten rows.
for (int i = 0; i <= 9; i++)
{
DataRow row = dataTable.NewRow();
row["Product description"] = "Product description " + i;
dataTable.Rows.Add(row);
}
dataTable.AcceptChanges();
StringWriter writer = new StringWriter();
dataTable.WriteXml(writer, XmlWriteMode.WriteSchema, false);
string dataTableXml = writer.ToString();
System.IO.File.WriteAllText(@"c:\temp\test.xml", dataTableXml);
//Deserialize back to data table.
StringReader stringReader = new StringReader(dataTableXml);
DataTable newDataTasble = new DataTable();
newDataTasble.ReadXml(stringReader);
}
}
}
Output from this looks like the following...
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="MerricksTableName" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="MerricksTableName">
<xs:complexType>
<xs:sequence>
<xs:element name="ProductId" msdata:AutoIncrement="true" type="xs:int" minOccurs="0" />
<xs:element name="Product_x0020_description" msdata:ReadOnly="true" msdata:Expression="ProductId + ProductId" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<MerricksTableName>
<ProductId>0</ProductId>
<Product_x0020_description>0</Product_x0020_description>
</MerricksTableName>
<MerricksTableName>
<ProductId>1</ProductId>
<Product_x0020_description>2</Product_x0020_description>
</MerricksTableName>
<MerricksTableName>
<ProductId>2</ProductId>
<Product_x0020_description>4</Product_x0020_description>
</MerricksTableName>
<MerricksTableName>
<ProductId>3</ProductId>
<Product_x0020_description>6</Product_x0020_description>
</MerricksTableName>
<MerricksTableName>
<ProductId>4</ProductId>
<Product_x0020_description>8</Product_x0020_description>
</MerricksTableName>
<MerricksTableName>
<ProductId>5</ProductId>
<Product_x0020_description>10</Product_x0020_description>
</MerricksTableName>
<MerricksTableName>
<ProductId>6</ProductId>
<Product_x0020_description>12</Product_x0020_description>
</MerricksTableName>
<MerricksTableName>
<ProductId>7</ProductId>
<Product_x0020_description>14</Product_x0020_description>
</MerricksTableName>
<MerricksTableName>
<ProductId>8</ProductId>
<Product_x0020_description>16</Product_x0020_description>
</MerricksTableName>
<MerricksTableName>
<ProductId>9</ProductId>
<Product_x0020_description>18</Product_x0020_description>
</MerricksTableName>
</NewDataSet>