Alternative approach to HTML generation
My program generates HTML via stringbuilder.
sbHTMLHeader.Append("<div id=\"printableContent\">");
sbHTMLHeader.Append("<div id=\"packingSlipHeader\">");
sbHTMLHeader.Append("<div class=\"floatLeft\开发者_如何学Go"><img src=\"../images/logo.gif\" alt=\"Logo\" /></div>");
//...
if (!orderType.Equals("L"))
{
sbHTMLHeader.Append(" <td>" + orderData.Tables[0].Rows[0]["carrier"] + "</td>" + Environment.NewLine);
//...
}
else
{
sbHTMLHeader.Append(" <td></td>" + Environment.NewLine);
//...
}
//...
380 lines later, I am passionitely against this code. In the past, I used Altova Stylevision. At my current job we have no license for the product. I don't need any of the PDF or other templates it provides, just HTML. Can you suggest a powerful, readable alternative to generate HTML such as above?
An alternative would be to store all the data in an XML file and write an XSLT (transformation sheet) that turns it into HTML.
As usually I can recommend: Html Agility Pack
In another approach you can use classes from System.Web.UI.HtmlControls namespace to create html.
You can definitely use NVelocity to help out with generating such templated HTML. The basic syntax is easy, and you can go quite complicated. Should save you a lot of tedious coding, and it doesn't require that you massage your input data beforehand.
精彩评论