用C#编程创建HTML网页 [英] Create HTML webpage programmatically in C#

查看:338
本文介绍了用C#编程创建HTML网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道:是否有一种方法可以用C#编程创建HTML文件,就像您可以使用XML一样? Mine是一个控制台应用程序,所以也许有些选项不可用。基本上,我想做一些比创建一个大字符串更聪明的东西。

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.

可能的场景:

取而代之写作:

Instead of writing:

     string html="<html><head>Blah</head><body>{0}</html>", myotherstring

我想在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.

推荐答案

您可以使用NVelocity。它是Java Velocity模板系统的.Net端口。该API不会与XmlWriter类似。相反,你会用简单的脚本语言编写一个文本文件,把你的对象放到一个上下文中,然后合并模板和上下文来生成HTML文件。

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.


  1. NVelocity

  2. Velocity语法指南

  1. NVelocity
  2. Velocity Syntax Guide

这篇关于用C#编程创建HTML网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆