1
\$\begingroup\$

My code can generate HTML table strings well, but it depends on JSON.NET.

I'm converting IEnumerable to an HTML table string using Json.NET but I think I shouldn't.

void Main()
{
    var datas = new[] { new {Name="Test"} };
    var array = datas.ToArray().ToHtmlTable(); //Run Success
    var set = datas.ToHashSet().ToHtmlTable();//Run Succes
    var list = datas.ToList().ToHtmlTable(); //Run Succes
    var enums = datas.AsEnumerable().ToHtmlTable(); //Run Succes
}

public static class HTMLTableHelper
{
    public static string ToHtmlTable(this IEnumerable enums)
    {
        return ToHtmlTableConverter(enums);
    }

    public static string ToHtmlTable(this System.Data.DataTable dataTable)
    {
        return ConvertDataTableToHTML(dataTable);
    }

    private static string ToHtmlTableConverter(object enums)
    {
        var jsonStr = JsonConvert.SerializeObject(enums);
        var data = JsonConvert.DeserializeObject<System.Data.DataTable>(jsonStr);
        var html = ConvertDataTableToHTML(data);
        return html;
    }

    private static string ConvertDataTableToHTML(System.Data.DataTable dt)
    {
        var html = new StringBuilder("<table>");

        //Header
        html.Append("<thead><tr>");
        for (int i = 0; i < dt.Columns.Count; i++)
            html.Append("<th>" + dt.Columns[i].ColumnName + "</th>");
        html.Append("</tr></thead>");

        //Body
        html.Append("<tbody>");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            html.Append("<tr>");
            for (int j = 0; j < dt.Columns.Count; j++)
                html.Append("<td>" + dt.Rows[i][j].ToString() + "</td>");
            html.Append("</tr>");
        }

        html.Append("</tbody>");
        html.Append("</table>");
        return html.ToString();
    }
}

I think it is bad because SerializeObject to string could be DeserializeObject to DataTable is a waste of efficiency.

var jsonStr = JsonConvert.SerializeObject(enums);
var data = JsonConvert.DeserializeObject<System.Data.DataTable>(jsonStr);

I want to remove dependencies on JSON.NET because it can reduce the size of the library.

Do you think I should replace them with something else if there is a better way?

\$\endgroup\$
13
  • \$\begingroup\$ mhmm... and yet you're using both JsonNet and DataTables :-| Could you reframe your question please? Currently this is very confusing. \$\endgroup\$
    – t3chb0t
    Commented Jan 16, 2019 at 7:47
  • 1
    \$\begingroup\$ @t3chb0t: others may not write the code for him, but they could still point him in the right direction... \$\endgroup\$ Commented Jan 16, 2019 at 7:58
  • 2
    \$\begingroup\$ @ITWeiHan: read up on reflection. Also, Json.NET is open-source, so you can take a look at how DeserializeObject is implemented. \$\endgroup\$ Commented Jan 16, 2019 at 7:59
  • 1
    \$\begingroup\$ @PieterWitvoet as a matter of fact this could be even a valid Code Review question if asked differently... IT WeiHan you may usk us what we think of the JsonNet and DataTable dependency and why you think it's bad and what alternatives there might be or in which way would we improve that? I think this would be good question. But you have to tell us your reasoning etc. Simply saying I cannot use it is not enough. \$\endgroup\$
    – t3chb0t
    Commented Jan 16, 2019 at 8:08
  • 4
    \$\begingroup\$ I made a couple small changes to your question so it doesn't sound like please implement something else for me any more - I hope like the edit ;-) \$\endgroup\$
    – t3chb0t
    Commented Jan 16, 2019 at 8:33

1 Answer 1

3
\$\begingroup\$

I can use reflection to solve it:

void Main()
{
    var datas = new[] { new {Name="Test1",Value="Test2"} };
    var array = datas.ToArray().ToHtmlTable(); //Run Success
    var set = datas.ToHashSet().ToHtmlTable();//Run Succes
    var list = datas.ToList().ToHtmlTable(); //Run Succes
    var enums = datas.AsEnumerable().ToHtmlTable(); //Run Succes
}

public static class HTMLTableHelper
{
    public static string ToHtmlTable<T>(this IEnumerable<T> enums)
    {
        var type = typeof(T);
        var props = type.GetProperties();
        var html = new StringBuilder("<table>");

        //Header
        html.Append("<thead><tr>");
        foreach (var p in props)
            html.Append("<th>" + p.Name + "</th>");
        html.Append("</tr></thead>");

        //Body
        html.Append("<tbody>");
        foreach (var e in enums)
        {
            html.Append("<tr>");
            props.Select(s => s.GetValue(e)).ToList().ForEach(p => {
                html.Append("<td>" + p + "</td>");
            }); 
            html.Append("</tr>");
        }

        html.Append("</tbody>");
        html.Append("</table>");
        return html.ToString();
    }
}

<table><thead><tr><th>Name</th><th>Value</th></tr></thead><tbody><tr><td>Test1</td><td>Test2</td></tr></tbody></table>

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.