-2

I need to convert the contents of a generic list of a custom class to html. e.g., if I am storing values for a class such as the following in a generic list:

public class PlatypusSummary
{
    public String PlatypusName { get; set; }
    public int TotalOccurrencesOfSummaryItems { get; set; }
    public int TotalSummaryExceptions { get; set; }
    public double TotalPercentageOfSummaryExceptions { get; set; }
}

...so that I end up with a generic list like so:

List<PlatypusSummary> _PlatypusSummaryList = null;
. . .
var dbp = new PlatypusSummary
{
    PlatypusName = summary["duckbillname"].ToString(),
    TotalOccurrencesOfSummaryItems = totalOccurrences,
    TotalSummaryExceptions = totalExceptions,
    TotalPercentageOfSummaryExceptions = totalPercentage
};
_PlatypusSummaryList.Add(dbp);

...how can I convert the contents of that generic list to HTML?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • Although I see you're trying to provide a question and answer to help others, your question itself is too broad and therefore doesn't fit on Stack Overflow. – mason Jan 04 '16 at 18:17
  • Probably unsurprisingly, I disagree; the solution is easily adaptable to any generic list. – B. Clay Shannon-B. Crow Raven Jan 04 '16 at 18:19
  • Your question should be able to stand on its own merit. And it can't, because it's too broad. Think of all the ways this could be done - there's dozens of ways to generate HTML from some list of objects. Someone that legitimately asked what you just asked, I would close as too broad and tell them to research their options, pick one, implement it, and only then come to Stack Overflow if they get stuck on their implementation. – mason Jan 04 '16 at 18:22

1 Answers1

-1

Here's a way to generate some "plain vanilla" HTML from that list. This can be adapted for other class types (replace "PlatypusSummary" with your class) and class members (replace "PlatypusName" and the other values in the foreach loop for the members of your class):

internal static string ConvertDuckbillSummaryListToHtml(List<PlatypusSummary> _PlatypusSummaryList)
{
    StringBuilder builder = new StringBuilder();
    // Add the html opening tags and "preamble"
    builder.Append("<html>");
    builder.Append("<head>");
    builder.Append("<title>");
    builder.Append("bla"); // TODO: Replace "bla" with something less blah
    builder.Append("</title>");
    builder.Append("</head>");
    builder.Append("<body>");
    builder.Append("<table border='1px' cellpadding='5' cellspacing='0' ");
    builder.Append("style='border: solid 1px Silver; font-size: x-small;'>");

    // Add the column names row
    builder.Append("<tr align='left' valign='top'>");
    PropertyInfo[] properties = typeof(PlatypusSummary).GetProperties();
    foreach (PropertyInfo property in properties)
    {
        builder.Append("<td align='left' valign='top'><b>");
        builder.Append(property.Name);
        builder.Append("</b></td>");
    }
    builder.Append("</tr>");

    // Add the data rows
    foreach (PlatypusSummary ps in _PlatypusSummaryList)
    {
        builder.Append("<tr align='left' valign='top'>");

        builder.Append("<td align='left' valign='top'>");
        builder.Append(ps.PlatypusName);
        builder.Append("</td>");

        builder.Append("<td align='left' valign='top'>");
        builder.Append(ps.TotalOccurrencesOfSummaryItems);
        builder.Append("</td>");

        builder.Append("<td align='left' valign='top'>");
        builder.Append(ps.TotalSummaryExceptions);
        builder.Append("</td>");

        builder.Append("<td align='left' valign='top'>");
        builder.Append(ps.TotalPercentageOfSummaryExceptions);
        builder.Append("</td>");

        builder.Append("</tr>");
    }

    // Add the html closing tags
    builder.Append("</table>");
    builder.Append("</body>");
    builder.Append("</html>");

    return builder.ToString();
}

Note: This code was adapted from Sumon Banerjee's "Data Table to HTML" code here.

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862