【问题标题】:How can I convert the contents of a generic List to HTML?如何将通用列表的内容转换为 HTML?
【发布时间】:2016-01-05 02:11:01
【问题描述】:

我需要将自定义类的通用列表的内容转换为 html。例如,如果我将以下类的值存储在通用列表中:

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

...所以我最终得到一个像这样的通用列表:

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

...如何将该通用列表的内容转换为 HTML?

【问题讨论】:

  • 虽然我看到您试图提供问题和答案来帮助他人,但您的问题本身过于宽泛,因此不适合 Stack Overflow。
  • 不出所料,我不同意;该解决方案很容易适应任何通用列表。
  • 您的问题应该能够站得住脚。它不能,因为它太宽泛了。想一想可以做到这一点的所有方法——有几十种方法可以从一些对象列表生成 HTML。有人合法地问你刚才问了什么,我会认为过于宽泛,并告诉他们研究他们的选择,选择一个,实施它,然后如果他们在实施过程中遇到困难,才来 Stack Overflow。

标签: c# html dom generic-list


【解决方案1】:

这是一种从该列表生成一些“普通”HTML 的方法。这可以适用于其他类类型(将“PlatypusSummary”替换为您的类)和类成员(将“PlatypusName”和 foreach 循环中的其他值替换为您的类成员):

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();
}

注意:此代码改编自 Sumon Banerjee 的“数据表到 HTML”代码here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 2011-08-13
    • 2017-05-29
    • 2013-01-03
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    相关资源
    最近更新 更多