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 _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?
Talk1:
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.
Talk2:
Probably unsurprisingly, I disagree; the solution is easily adaptable to any generic list.
Talk3:
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.
Solutions1
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 _PlatypusSummaryList)
{
StringBuilder builder = new StringBuilder();
// Add the html opening tags and "preamble"
builder.Append("");
builder.Append("
");builder.Append("
");builder.Append("bla"); // TODO: Replace "bla" with something less blah
builder.Append("
");builder.Append("");
builder.Append("
");builder.Append("
builder.Append("style='border: solid 1px Silver; font-size: x-small;'>");
// Add the column names row
builder.Append("
");PropertyInfo[] properties = typeof(PlatypusSummary).GetProperties();
foreach (PropertyInfo property in properties)
{
builder.Append("
");builder.Append(property.Name);
builder.Append("
");}
builder.Append("
");// Add the data rows
foreach (PlatypusSummary ps in _PlatypusSummaryList)
{
builder.Append("
");builder.Append("
");builder.Append(ps.PlatypusName);
builder.Append("
");builder.Append("
");builder.Append(ps.TotalOccurrencesOfSummaryItems);
builder.Append("
");builder.Append("
");builder.Append(ps.TotalSummaryExceptions);
builder.Append("
");builder.Append("
");builder.Append(ps.TotalPercentageOfSummaryExceptions);
builder.Append("
");builder.Append("
");}
// Add the html closing tags
builder.Append("
");builder.Append("");
builder.Append("");
return builder.ToString();
}
Note: This code was adapted from Sumon Banerjee's "Data Table to HTML" code here.