Click or drag to resize
Json.NETSerialize a DataSet
 

This sample serializes a DataSet to JSON.

Sample
Usage
 1DataSet dataSet = new DataSet("dataSet");
 2dataSet.Namespace = "NetFrameWork";
 3DataTable table = new DataTable();
 4DataColumn idColumn = new DataColumn("id", typeof(int));
 5idColumn.AutoIncrement = true;
 6
 7DataColumn itemColumn = new DataColumn("item");
 8table.Columns.Add(idColumn);
 9table.Columns.Add(itemColumn);
10dataSet.Tables.Add(table);
11
12for (int i = 0; i < 2; i++)
13{
14    DataRow newRow = table.NewRow();
15    newRow["item"] = "item " + i;
16    table.Rows.Add(newRow);
17}
18
19dataSet.AcceptChanges();
20
21string json = JsonConvert.SerializeObject(dataSet, Formatting.Indented);
22
23Console.WriteLine(json);
24// {
25//   "Table1": [
26//     {
27//       "id": 0,
28//       "item": "item 0"
29//     },
30//     {
31//       "id": 1,
32//       "item": "item 1"
33//     }
34//   ]
35// }