在 C# 中以编程方式创建 HTML 网页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/937201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create HTML webpage programmatically in C#
提问by Dervin Thunk
I was wondering: is there a way to create HTML files programmatically in C# as you can do with XML? Mine is a console application, so maybe some of the options are not available. Basically, I would like to do something smarter than just building a big string.
我想知道:有没有办法像使用 XML 一样在 C# 中以编程方式创建 HTML 文件?我的是一个控制台应用程序,所以可能有些选项不可用。基本上,我想做一些比构建大字符串更聪明的事情。
Possible scenario:
可能的场景:
Instead of writing:
而不是写:
string html="<html><head>Blah</head><body>{0}</html>", myotherstring
I would like to work as in XML
我想在 XML 中工作
XmlTextWriter w = new XmlTextWriter(xml_file_path + xml_file_name,
System.Text.Encoding.UTF8);
w.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
// construct xml
XmlElement root = xmlDoc.CreateElement("element");
...
xmlDoc.Save(w);
w.Close();
Apologies for the naive question.
为幼稚的问题道歉。
采纳答案by Lorin
You could use NVelocity. It is a .Net port of the Java Velocity templating system. The API will not be similar to XmlWriter. Instead, you'll write a text file in a simple scripting language, put your objects into a 'context' and then merge the template and the context to generate the HTML file.
你可以使用NVelocity。它是 Java Velocity 模板系统的 .Net 端口。API 将与 XmlWriter 不同。相反,您将使用简单的脚本语言编写一个文本文件,将您的对象放入“上下文”中,然后合并模板和上下文以生成 HTML 文件。
回答by Carlo
What I did a few months back, I had an asp.net file (aspx) saved as a template in a text file, whenever the user needed a new page, I would just copy that template into the user specified folder, change the extension .txt to .aspx, and programmatically add a few options depending on the user's needs. It was a simple page though. Of course, the more complex you go, the more complex the code will be.
几个月前我做了什么,我有一个 asp.net 文件(aspx)作为模板保存在一个文本文件中,每当用户需要一个新页面时,我只需将该模板复制到用户指定的文件夹中,更改扩展名.txt 到 .aspx,并根据用户的需要以编程方式添加一些选项。虽然这是一个简单的页面。当然,越复杂,代码就越复杂。
回答by Cheeso
Don't forget: You can generate XHTML just as easily as plain XML using the XmlTextWriter approach.
不要忘记:您可以使用 XmlTextWriter 方法像生成纯 XML 一样轻松地生成 XHTML。
回答by Sergey Shandar
You could use some third party open-source libraries to generated strong typed verified (X)HTML, such as CityLizard Frameworkor Sharp DOM.
您可以使用一些第三方开源库来生成强类型验证 (X)HTML,例如CityLizard Framework或 Sharp DOM。
For example
例如
html
[head
[title["Title of the page"]]
[meta_(
content: "text/html;charset=UTF-8",
http_equiv: "Content-Type")
]
[link_(href: "css/style.css", rel: "stylesheet", type: "text/css")]
[script_(type: "text/javascript", src: "/JavaScript/jquery-1.4.2.min.js")]
]
[body
[div
[h1["Test Form to Test"]]
[form_(action: "post", id: "Form1")
[div
[label["Parameter"]]
[input_(type: "text", value: "Enter value")]
[input_(type: "submit", value: "Submit!")]
]
]
[div
[p["Textual description of the footer"]]
[a_(href: "http://google.com/")
[span["You can find us here"]]
]
[div["Another nested container"]]
]
]
];
回答by Justin
I realise that this question is old, however the recent release of the ASP.Net MVC 3 Razor view engine now gives you the option to use this same Razor view engine to generate HTML for any purpose.
我意识到这个问题很老,但是最近发布的 ASP.Net MVC 3 Razor 视图引擎现在让您可以选择使用相同的 Razor 视图引擎来为任何目的生成 HTML。
See Hosting Razor outside of ASP.Netfor a guide on how to do this.
有关如何执行此操作的指南,请参阅在 ASP.Net 之外托管 Razor。