Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c控制台应用程序中通过App.config文件控制静态html页面的动态html表的样式?_C#_Html_App Config - Fatal编程技术网

C# 如何在c控制台应用程序中通过App.config文件控制静态html页面的动态html表的样式?

C# 如何在c控制台应用程序中通过App.config文件控制静态html页面的动态html表的样式?,c#,html,app-config,C#,Html,App Config,我一直想做点什么。我有一个控制台应用程序,它查找一些文件,如果它们存在或不存在。因此,此应用程序将向我的电子邮件id发送电子邮件。将为电子邮件格式创建一个普通的SuccessBody.html 现在,我尝试在message.html中动态添加一个html表,如下所示 HTML 我不想在代码中设计这个表,而是通过配置文件。其中最关键的部分是循环设计。 $Title$、$Column$和$Rows$是我想用一些c代码替换的字符串,如下所示: C 我想在配置文件AppConfig.xml中配置此表的设

我一直想做点什么。我有一个控制台应用程序,它查找一些文件,如果它们存在或不存在。因此,此应用程序将向我的电子邮件id发送电子邮件。将为电子邮件格式创建一个普通的SuccessBody.html

现在,我尝试在message.html中动态添加一个html表,如下所示

HTML

我不想在代码中设计这个表,而是通过配置文件。其中最关键的部分是循环设计。 $Title$、$Column$和$Rows$是我想用一些c代码替换的字符串,如下所示:

C

我想在配置文件AppConfig.xml中配置此表的设置,并在上面的代码中使用它

配置文件

<add key="FileName" value="File Name"/>
    <add key="Date" value="Creation Date"/>
    <add key ="ForColumns" value="&lt;td style=\&quot; border-color:#FFFFFF; border-style:solid; border-width:thick; padding: 5px;\&quot;&gt;"/>
    <add key="ForRows" value="&lt;tr style =\&quot;color:#17202A; font-weight:bold; font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif;\&quot; &gt;"/>
    <add key="RowData" value="&lt;td style=\&quot; border-color:#FFFFFF; background-color:#F3E2A9; border-style:solid; border-width:thick; padding: 5px;\&quot;&gt;"/>
    <add key="Body_Success" value="HTMLBody\SuccessBody.html"/>
Body_Success是SuccessBody.html=应用程序位置的位置


$Title$很容易被替换。但是,$Column$和$Rows$并没有像我希望的那样被替换。如果有人能帮助我,那就太好了。

在.Net和许多其他工具堆栈中,字符串应该是不可变的。这意味着,一旦您完成以下操作:

I would suggest - 
Create html file and save email html part along with keywords which you want to replace like as ForColumns ,ForRows.
e.g. 
<html>
  <body>
    <table>
      <thead>
       <th>ForColumns1 </th>
       <th>ForColumns2 </th>
      </thead>
     <tr>ForRows1</tr>
     <tr>ForRows2</tr>
   </table>
  </body>
</html>


while sending mail get replace keyword(ForColumns,ForRows) value from config and replace with html file data.
string foo = "bar";
如果您检查var foo,您将始终找到bar。如果您希望foo成为其他对象,则必须为其指定一个新值:

string foo = "fubar";
string huh = foo.Replace("fu", "foo");
同样的原则也适用于对字符串调用的方法,比如Replace。该方法返回一个新字符串,其中用新值替换了原来的sting。所以在上面的例子中,huh的值为foobar,其中foo仍然是fubar

因此,在这一行:

File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]).Replace("$Title$", ConfigTitle);
writehtmlFile.WriteLine(html);
你把替换的结果扔到地板上了

而是这样做:

// read the template from the file once and put it in a variable
email_body_success = File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]);
// replace the title by storing the replaced value again the same variable
email_body_success = email_body_success.Replace("$Title$", ConfigTitle);
从这一点开始,email\u body\u success将使用部分替换模板的字符串。您不需要再次调用File.ReadAlltext,这样做是错误的。因为如果您这样做,您将再次从文件中读取模板,丢弃已替换的内容

现在我们来讨论两个循环,即列和行。一旦你建立了你需要的东西,html的内容就需要用email\u body\u success中的标记来替换

而不是:

File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]).Replace("$Columns$", html);

因为现在email_body_success将使用$Columns$的新html部分替换以前的模板。在行循环开始时,您希望再次将html字符串重置为空,以便它不再保留列

在行循环的末尾,需要应用相同的修复。替换

email_body_success = File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]).Replace("$Rows$", html);

这将在电子邮件中为您提供模板及其替换部件

我不确定你想用这一行实现什么:

File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]).Replace("$Title$", ConfigTitle);
writehtmlFile.WriteLine(html);

但是,如果您打算将整个模板写入该文件,则需要使用email_body_success变量替换HTMLW,并在上次替换后移动这些行

返回一个字符串。你根本不储存它。IIRC编译器警告过你,是的@雷内。我还研究了代码,并认为如果我采用这种方法,我就不需要streamwriter来编写html文件。但还不够肯定。
 // replace the rows
 email_body_success = email_body_success.Replace("$Rows$", html);
writehtmlFile.WriteLine(html);