作为介绍,我使用MS Word创建了一个文档,然后保存为html文档。在C#中,我构建了一个无序的html列表(使用MS Word格式(,然后通过替换特定的标记将其添加到html文档中。
我在下面的字符串变量unorderedHtmlList最初初始化为空字符串。然后我连接html字符串,并替换一些由"[["one_answers"]]"字符括起来的标记。由于某些原因,当我应用Replace时,它不会用新值替换项[[fieldName]和[[fieldValue]]。参见以下代码:
string unorderedHtmlList = string.Empty;
foreach (System.Data.DataRow row in myDataTable.Rows)
{
string name = row["fieldName"].ToString();
string value = row["fieldValue"].ToString();
unorderedHtmlList += "<p style='margin-left:36.0pt;text-align:justify;text-indent:-18.0pt;" +
"line-height:125%;mso-list:l1 level1 lfo3'><![if !supportLists]><span" +
"style='font-size:10.5pt;line-height:125%;font-family:"Arial",sans-serif;" +
"mso-fareast-font-family:Arial;color:#222222'><span" +
"style='mso-list:Ignore'>-<span style='font:7.0pt "Times New Roman"'> " +
"</span></span></span><![endif]><span style='font-size:10.5pt;" +
"line-height:125%;font-family:"Arial",sans-serif;color:#222222'>[[fieldName]]" +
"</span><span style='font-size:10.5pt;line-height:125%;font-family:" +
""Helvetica",sans-serif;color:#222222'>[[fieldValue]]</span><span" +
"style='font-size:10.5pt;line-height:125%;font-family:"Arial",sans-serif;" +
"color:#222222'><o:p></o:p></span></p>".Replace("[[fieldName]]", name).Replace("[[fieldValue]]", value);
}
有什么想法为什么替换不起作用吗?
您正在连接字符串,并且替换操作仅在最后一部分执行。
"color:#222222'><o:p></o:p></span></p>".Replace("[[fieldName]]", name).Replace("[[fieldValue]]", value);
试试这个:
unorderedHtmlList += ("<p style='margin-left:36.0pt;text-align:justify;text-indent:-18.0pt;" +
"line-height:125%;mso-list:l1 level1 lfo3'><![if !supportLists]><span" +
"style='font-size:10.5pt;line-height:125%;font-family:"Arial",sans-serif;" +
"mso-fareast-font-family:Arial;color:#222222'><span" +
"style='mso-list:Ignore'>-<span style='font:7.0pt "Times New Roman"'> " +
"</span></span></span><![endif]><span style='font-size:10.5pt;" +
"line-height:125%;font-family:"Arial",sans-serif;color:#222222'>[[fieldName]]" +
"</span><span style='font-size:10.5pt;line-height:125%;font-family:" +
""Helvetica",sans-serif;color:#222222'>[[fieldValue]]</span><span" +
"style='font-size:10.5pt;line-height:125%;font-family:"Arial",sans-serif;" +
"color:#222222'><o:p></o:p></span></p>").Replace("[[fieldName]]", name).Replace("[[fieldValue]]", value);