Kurru
Kurru

Reputation: 14331

What is the best way of using this string value in c#?

Whats the best way to make the following HTML text block a hardcoded string object in c#? I need it for a test can't use the @ operator since it just stops at the first ". Any ideas?

====================================================

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>

<HEAD>

  <META http-equiv="Content-Type" content="text/html; charset=utf-8">

  <TITLE>Example Web Page</TITLE>

</HEAD> 

<body>  

<p>You have reached this web page by typing &quot;example.com&quot;,

&quot;example.net&quot;,&quot;example.org&quot

  or &quot;example.edu&quot; into your web browser.</p>

<p>These domain names are reserved for use in documentation and are not available 

  for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC 

  2606</a>, Section 3.</p>

</BODY>

</HTML>

Upvotes: 0

Views: 123

Answers (4)

RKh
RKh

Reputation: 14161

Probably this link might answer your question:

Storing HTML tags in a string variable.

Upvotes: 0

Nobody
Nobody

Reputation: 4841

If you don't want to embed it like Jon Skeet's answer, you'll have to change all occurences of " to "".

Upvotes: 1

David M&#229;rtensson
David M&#229;rtensson

Reputation: 7600

You can use @ but you have to escape any " as "" (double)

There is no other way as far as I have found.

First lines would then be

@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"">

<HTML>

Upvotes: 6

Jon Skeet
Jon Skeet

Reputation: 1500525

I would suggest you put it in a text file that you then embed into the assembly. That way you can edit it with syntax highlighting etc, and it won't clutter up your code.

Use Assembly.GetManifestResourceStream to load it again at execution time. If you do this a fair amount, you might want to write helper methods to do it more easily.

Upvotes: 5

Related Questions