Reputation: 51241
I need to store HTML code for an email template in a variable, so I want to load raw HTML source into a string
. This obviously doesn't work if I can't escape its contents.
For instance, I can't do this:
string myHtml = "<html><body>
<a href="foo.bar" class="blap">blip</a>
</body></html>";
How can I store HTML code as a native C# string?
Upvotes: 16
Views: 47905
Reputation: 415755
Old, but C# now also has a Raw String Literal feature to make this even easier:
string myHtml = """<html><body>
<a href="foo.bar" class="blap">blip</a>
</body></html>""";
No need to worry about the quotes in the string, and you can use any number of starting/ending quotes if for some reason you have text already containing sets of quotes together.
Upvotes: 0
Reputation: 65496
If you html code is in a file you can File.ReadAllText
string myString = System.IO.File.ReadAllText("path to the file");
Upvotes: 7
Reputation: 1062780
Strings and HTML should be fine. The only complication is escaping, which is made easier using verbatim string literals (@"..."). Then you just double any double-quotes:
string body = @"<html><body>
<a href=""foo.bar"" class=""blap"">blip</a>
...
</body></html>";
Upvotes: 35