【发布时间】:2011-04-03 20:13:32
【问题描述】:
我的 winform 应用程序中有 4 个从我的数据库中获取值的数据集。诸如我的产品表中有多少产品以及有关类别的信息之类的值。我想知道如何将数据集信息保存在 html 页面中。我想创建一个 html 模板,以便我可以以很好的方式呈现信息。我怎样才能做到这一点?有什么好的指南可以解释如何做到这一点?
【问题讨论】:
我的 winform 应用程序中有 4 个从我的数据库中获取值的数据集。诸如我的产品表中有多少产品以及有关类别的信息之类的值。我想知道如何将数据集信息保存在 html 页面中。我想创建一个 html 模板,以便我可以以很好的方式呈现信息。我怎样才能做到这一点?有什么好的指南可以解释如何做到这一点?
【问题讨论】:
对于RazorEngine 这样的公司来说,这似乎是一项很棒的工作。您可以使用 Razor 语法定义模板,然后使用 RazorEngine 模板服务渲染出内容,例如:
@helper RenderItem(Item item) {
<tr>
<td>item.Name</td>
<td>item.Price</td>
</tr>
}
<html>
<head></head>
<body>
<table>
@foreach (Item item in Model.Items) {
@RenderItem(item)
}
</table>
</body>
</html>
【讨论】:
您可以将DataSet 保存为XML,然后使用XSLT 对其进行转换。
您可以查看以下示例:
【讨论】:
我个人建议使用 Linq to Xml 生成 HTML(使用 System.Xml.Linq)
您甚至不必使用严格的 XHTML 模式,但您会从 Xml.Linq 中获得大量数据。这是我自己的代码库中的一个 sn-p:
#region Table Dump Implementation
private static XNode Dump<T>(IEnumerable<T> items, IEnumerable<string> header, params Func<T, string>[] columns)
{
if (!items.Any())
return null;
var html = items.Aggregate(new XElement("table", new XAttribute("border", 1)),
(table, item) => {
table.Add(columns.Aggregate(new XElement("tr"),
(row, cell) => {
row.Add(new XElement("td", EvalColumn(cell, item)));
return row;
} ));
return table;
});
html.AddFirst(header.Aggregate(new XElement("tr"),
(row, caption) => { row.Add(new XElement("th", caption)); return row; }));
return html;
}
private static XNode EvalColumn<T>(Func<T, string> cell, T item)
{
var raw = cell(item);
try
{
var xml = XElement.Parse(raw);
return xml;
}
catch (XmlException)
{
return new XText(raw);
}
}
#endregion
#region Dot Diagrams
public void LinkDiagram(Digraph graph, string id)
{
if (!graph.AllNodes.Any())
return;
var img = Path.GetFileName(GenDiagramFile(graph, _directory, id));
_body.Add(
new XElement("a",
new XAttribute("href", img),
new XElement("h4", "Link naar: " + graph.name),
new XElement("img",
new XAttribute("border", 1),
new XAttribute("src", img),
new XAttribute("width", "40%"))));
}
请注意,使用内联 HTML 文本也非常容易(只要它是有效的 XML),使用这样的帮助器:
public void GenericAppend(string content)
{
if (!string.IsNullOrEmpty(content))
_body.Add(XElement.Parse(content));
}
【讨论】:
您想要的非常简单,因此您可能只想直接生成 html 并使用一些预先创建的 CSS 进行样式设置。但是,如果您想要更复杂的东西,请查看Windward Reports。
【讨论】: