在C#应用程序中生成HTML页面

时间:2011-10-06 04:27:38

标签: c# html string

我正在编写一个需要生成HTML页面的C#应用​​程序。只有部分HTML(例如标题)需要由C#app定义。基本标记等是静态的。

给出标题的例子,目前我正是这样做的。 我将基本的html保存为txt文件。这是一部分:

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
        <title>Title goes here</title>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>

我在C#应用程序中将其读入字符串(线框),然后更改标题,我做了

oldtitle = "<title>Title goes here</title>";
newtitle = "<title>This is the title I want</title>";
wireframe = wireframe.Replace(oldtite,newtitle);

以上只是一个例子。字符串线框实际上由整个html代码组成。我想知道我这样做的方式是否有效?我猜不是因为要更改标题,我正在搜索整个字符串。

实现这一目标的更有效方法是什么?

3 个答案:

答案 0 :(得分:4)

我有类似的任务。我有一个恒定的html结构,我需要粘贴数据。 我定义了XSLT,当接收数据时我做了XSLT转换。 它工作得很快。

答案 1 :(得分:0)

您可以使用IndexOf Method只查找一次,例如:

int index = wireframe.IndexOf(oldtitle);
wireframe = wireframe.Substring(0, index) + newtitle + wireframe.Substring(index + oldtitle.length);

答案 2 :(得分:0)

我相信你可以完成你在尝试使用String.Format

做的事情

http://msdn.microsoft.com/en-us/library/system.string.format.aspx

模板文件可能如下所示:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
    <title>{0}</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>

然后

string input = ... // <-- read the "template" file into this string
string output = String.Format(input, newTitle);

我相信这会更有效率。