C# 什么';生成HTML文件的最简单方法是什么?
我正在开发一个控制台应用程序,该应用程序应该能够生成一个包含一个表和一些javascript的html文档 我考虑手工编写html:C# 什么';生成HTML文件的最简单方法是什么?,c#,html,.net,console-application,C#,Html,.net,Console Application,我正在开发一个控制台应用程序,该应用程序应该能够生成一个包含一个表和一些javascript的html文档 我考虑手工编写html: streamWriter.WriteLine("<html>"); streamWriter.WriteLine("<body>"); streamWriter.WriteLine(GetHtmlTable()); streamWriter.WriteLine("</body>"); streamWriter.WriteLine
streamWriter.WriteLine("<html>");
streamWriter.WriteLine("<body>");
streamWriter.WriteLine(GetHtmlTable());
streamWriter.WriteLine("</body>");
streamWriter.WriteLine("</html>");
假设我的思路正确,那么使用
System.Web.UI.Page
类构建html文档其余部分(即:html、head、title、body元素)的正确方法是什么?我需要使用文字控件吗?最好使用模板系统来解耦表示和业务逻辑
请看Razor Generator,它允许在非ASP.NET应用程序中使用CSHTML模板
我做了很多自动生成HTML页面的工作。我喜欢创建一个带有自定义标记的HTML页面模板,在其中插入动态控件、数据或文字。然后,我将模板文件读入一个字符串,并用生成的HTML替换自定义标记,就像上面所做的那样,然后将HTML文件从字符串中写回。这节省了我为设计模板、css和支持JS创建所有繁琐的HTML支持的时间 模板文件示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<CUSTOMHEAD />
</head>
<body>
<CUSTOMDATAGRID />
</body>
</html>
从加载到字符串示例中的模板文件创建HTML
private void GenerateHTML(string TemplateFile, string OutputFileName)
{
string strTemplate = TemplateFile;
string strHTMLPage = "";
string strCurrentTag = "";
int intStartIndex = 0;
int intEndIndex = 0;
while (strTemplate.IndexOf("<CUSTOM", intEndIndex) > -1)
{
intStartIndex = strTemplate.IndexOf("<CUSTOM", intEndIndex);
strHTMLPage += strTemplate.Substring(intEndIndex,
intStartIndex - intEndIndex);
strCurrentTag = strTemplate.Substring(intStartIndex,
strTemplate.IndexOf("/>", intStartIndex) + 6 - intStartIndex);
strCurrentTag = strCurrentTag.ToUpper();
switch (strCurrentTag)
{
case "<CUSTOMHEAD />":
strHTMLPage += GenerateHeadJavascript();
break;
case "<CUSTOMDATAGRID />":
StringWriter sw = new StringWriter();
GridView.RenderControl(new HtmlTextWriter(sw));
strHTMLPage += sw.ToString();
sw.Close();
break;
case "<CUSTOMANYOTHERTAGSYOUMAKE />":
//strHTMLPage += YourControlsRenderedAsString();
break;
}
intEndIndex = strTemplate.IndexOf("/>", intStartIndex) + 2;
}
strHTMLPage += strTemplate.Substring(intEndIndex);
try
{
StreamWriter swHTMLPage = new System.IO.StreamWriter(
OutputFileName, false, Encoding.UTF8);
swHTMLPage.Write(strHTMLPage);
swHTMLPage.Close();
}
catch (Exception ex)
{
// AppendLog("Write File Failed: " + OutputFileName + " - " + ex.Message);
}
}
private void GenerateHTML(字符串模板文件,字符串输出文件名)
{
字符串strTemplate=模板文件;
字符串strHTMLPage=“”;
字符串strCurrentTag=“”;
int intStartIndex=0;
int intEndIndex=0;
而(strTemplate.IndexOf(“,intStartIndex)+2;
}
strHTMLPage+=strTemplate.Substring(intEndIndex);
尝试
{
StreamWriter swHTMLPage=new System.IO.StreamWriter(
OutputFileName,false,Encoding.UTF8);
swHTMLPage.Write(strHTMLPage);
swHTMLPage.Close();
}
捕获(例外情况除外)
{
//AppendLog(“写入文件失败:“+OutputFileName+”-“+ex.Message”);
}
}
你可能会发现System.Web.Mvc.HtmlHelper
很有用。John你能澄清一下你在哪里构建这个应用程序吗?它是控制台应用程序
还是Web应用程序
第二个例子你做得对。我以前是这样做的(我没有代码)。
private void GenerateHTML(string TemplateFile, string OutputFileName)
{
string strTemplate = TemplateFile;
string strHTMLPage = "";
string strCurrentTag = "";
int intStartIndex = 0;
int intEndIndex = 0;
while (strTemplate.IndexOf("<CUSTOM", intEndIndex) > -1)
{
intStartIndex = strTemplate.IndexOf("<CUSTOM", intEndIndex);
strHTMLPage += strTemplate.Substring(intEndIndex,
intStartIndex - intEndIndex);
strCurrentTag = strTemplate.Substring(intStartIndex,
strTemplate.IndexOf("/>", intStartIndex) + 6 - intStartIndex);
strCurrentTag = strCurrentTag.ToUpper();
switch (strCurrentTag)
{
case "<CUSTOMHEAD />":
strHTMLPage += GenerateHeadJavascript();
break;
case "<CUSTOMDATAGRID />":
StringWriter sw = new StringWriter();
GridView.RenderControl(new HtmlTextWriter(sw));
strHTMLPage += sw.ToString();
sw.Close();
break;
case "<CUSTOMANYOTHERTAGSYOUMAKE />":
//strHTMLPage += YourControlsRenderedAsString();
break;
}
intEndIndex = strTemplate.IndexOf("/>", intStartIndex) + 2;
}
strHTMLPage += strTemplate.Substring(intEndIndex);
try
{
StreamWriter swHTMLPage = new System.IO.StreamWriter(
OutputFileName, false, Encoding.UTF8);
swHTMLPage.Write(strHTMLPage);
swHTMLPage.Close();
}
catch (Exception ex)
{
// AppendLog("Write File Failed: " + OutputFileName + " - " + ex.Message);
}
}