SerializableDataTable is a library providing an abstraction class that allows you to serialize and deserialize data to and from a DataTable instance.
If you have any issues or feedback, please file an issue here in Github. We'd love to have you help by contributing code for new features, optimization to the existing codebase, ideas for future releases, or fixes!
- Initial release
- Added markdown support
Refer to the Test project for exercising the library. This is example is using the NuGet package SerializationHelper for simplicity purposes.
using System.Data;
using SerializableDataTables;
using SerializationHelper;
// Create your DataTable and columns
DataTable dt1 = new DataTable("Test");
dt1.Columns.Add("id", typeof(int));
dt1.Columns.Add("name", typeof(String));
// Create your DataRows
dt1.Rows.Add(1, "Hello");
dt1.Rows.Add(2, "World");
SerializableDataTable sdt = SerializableDataTable.FromDataTable(dt);
string json = Serializer.SerializeJson(sdt, true);
Console.WriteLine("JSON: " + Environment.NewLine + json + Environment.NewLine);
/*
{
"Name": "Test",
"Columns": [
{
"Name": "id",
"Type": "Int32"
},
{
"Name": "name",
"Type": "String"
}
],
"Rows": [
{
"id": 1,
"name": "Hello"
},
{
"id": 2,
"name": "World"
}
]
}
*/
DataTable dt2 = Serializer.DeserializeJson<SerializableDataTable>(json).ToDataTable();This library supports the following DataTable value types: string, Int32, Int64, decimal, double, float, bool, DateTime, DateTimeOffset, byte, byte[], char, Guid, and Object.
The SerializableDataTable class has a public method ToMarkdown() which returns a markdown string.
The static class MarkdownConverter can be used with a DataTable or a SerializableDataTable to produce a markdown string.
using System.Data;
using SerializableDataTables;
SerializableDataTable sdt = ...;
string md = sdt.ToMarkdown();
md = MarkdownConverter.Convert(sdt); // full markdown
Console.WriteLine(MarkdownConverter.ConvertHeaders(sdt)); // show only header row and separator row
foreach (string str in MarkdownConverter.IterateRows(sdt))
{
Console.Write(str); // print each data row, which has a newline separator already added
}Refer to CHANGELOG.md for version history.