gpt4 book ai didi

c# - 读入 HTML 文件并替换为变量

转载 作者:太空狗 更新时间:2023-10-30 01:24:27 27 4
gpt4 key购买 nike

我有一个 HTML 文件,它将作为我要发送的电子邮件的模板。 html 中有一些字段是可变的。我想知道是否有一种可靠的方法可以用变量替换 HTML 文件中的占位符。我知道我可以 string.Replace 所有这些,但这并不理想,因为我有很多变量。这是 html 文件的样子

<html>
<head>
<title></title>
</head>
<body>
<div>
Please read the Cruise Control Details Below<br>
<br>
<table width='100%'>
<tr>
<td width='100%' colspan='5'>
<font size='4'><b>Release Details</b></font>
</td>
</tr>
<tr>
<td width='20%'>
<b>RFC Ticket #</b>
</td>
<td>
%release.RFCTicket%
</td>
<td>
&nbsp;
</td>
<td>
&nbsp;
</td>
<td width='10%'>
&nbsp;
</td>
<td width='20%'>
<b>Project / Release Name</b>
</td>
<td width='20%'>
%release.ReleaseName%
</td>
</tr>
<tr>
<td width='20%'>
<b>Release Date</b>
</td>
<td width='20%'>
%release.ReleaseDateString%
</td>
<td>
&nbsp;
</td>
<td>
&nbsp;
</td>
<td width='10%'>
&nbsp;
</td>
<td width='20%'>
<b>Release Time</b>
</td>
<td width='20%'>
%release.ReleaseTimeString%
</td>
</tr>
<tr>
<td width='20%'>
<b>CAB Approval Status</b>
</td>
<td width='20%'>
%release.CABApproval%
</td>
</tr>
<tr>
<td width='100%' colspan='5'>
&nbsp;
</td>
</tr>
<tr>
<td width='100%' colspan='5'>
<font size='4'><b>Contact Information:</b></font>
</td>
</tr>
<tr>
<td width='20%'>
<b>Project / Team Lead</b>
</td>
<td width='20%'>
%release.TeamLead%
</td>
<td width='10%'>
&nbsp;
</td>
<td width='20%'>
<b>On Call DSE</b>
</td>
<td width='20%'>
%release.OnCallDSE%
</td>
</tr>
<tr>
<td width='20%'>
<b>Phone</b>
</td>
<td width='20%'>
%release.ContactInfo%
</td>
<td>
&nbsp;
</td>
<td>
&nbsp;
</td>
<td>
&nbsp;
</td>
<td width='10%'>
&nbsp;
</td>
<td width='20%'>
<b>Phone</b>
</td>
<td width='20%'>
%release.OnCallDSEContact%
</td>
</tr>
<tr>
<td>
&nbsp;
</td>
</tr>
<tr>
<td width='100%' colspan='5'>
<font size='4'><b>Migration Details:</b></font>
</td>
</tr>
<tr>
<td width='20%'>
<b>Deploy Dashboard</b>
</td>
<td width='20%'>
&nbsp;
</td>
<td width='10%'>
&nbsp;
</td>
<td width='20%'>
<td>
&nbsp;
</td>
<td>
&nbsp;
</td>
<b>Deploy Task</b>
</td>
<td width='20%'>
&nbsp;
</td>
</tr>
%createTaskTable(ParseSpecialInstuctions().Split('|'))%</table>
</div>

我想用代表它们的代码中的变量替换“%%”之间的值。我可以轻松

string.Replace("%release.RFCTicket%",release.RFCTicket);

但我认为这有点令人费解,因为我在文件中有大约 10 个变量。是否有任何内置方法可以满足我的要求?任何帮助将不胜感激,谢谢!

最佳答案

使用正则表达式查找匹配项。我相信适当的正则表达式应该是这样的:

%release.\S+%

从那里,您可以检查每个匹配项,并从匹配项中解析成员名称。从那里您可以通过反射从您的实例(在本例中为释放)获取成员的值,并进行字符串替换。

是这样的。它可以使用一些重构来消除冗余调用,我不知道它是否完全有效,但你明白了......

var regex = new Regex("%release.\S+%");
var match = r.Match(htmlText);
while (match.Success)
{
var value = match.Value;
var memberName = ParseMemberName(value); //Some code you write to parse out the member name from the match value
var propertyInfo = release.GetType().GetProperty(memberName);
var memberValue = propertyInfo.GetValue(release, null);
htmlText = htmlText.Replace(value, memberValue != null ? memberValue.ToString() : string.Empty);
match = match.NextMatch();
}

关于c# - 读入 HTML 文件并替换为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9330412/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com