如何将html代码加载到c#string中?

use*_*652 10 html c# string

嗨,我有一个非常认真的问题.

我需要发送HTML代码,所以我必须将html加载到字符串中,如果我不能很好地解释它们,这显然不起作用,我怎么能本地存储它?

谢谢你的帮助

Mar*_*ell 27

字符串和HTML应该没问题.唯一的复杂因素是转义,使用逐字字符串文字(@"...")可以更容易.然后你只需加倍任何双引号:

string body = @"<html><body>
<a href=""foo.bar"" class=""blap"">blip</a>
...
</body></html>";
Run Code Online (Sandbox Code Playgroud)

  • 所以@""被称为逐字字符串文字?我要投票,因为到目前为止我一直在说`在引号前使用@(at)符号.谢谢@Marc. (2认同)

Pre*_*gha 6

如果你的html代码在文件中,你可以File.ReadAllText

string myString = System.IO.File.ReadAllText("path to the file");
Run Code Online (Sandbox Code Playgroud)