如何将内容转换为html,如何将通用列表的内容转换为HTML?

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值