views:

46

answers:

7

Hi there,

I have the following HTML code:

<P>Notes:&nbsp;&nbsp; Mails: <BR> &nbsp; 1. <A href="mailto:[email protected]">[email protected]</A></P>

and of course when I try to pass it to string it gives me error:

 string s =  "<P>Notes:&nbsp;&nbsp; Mails: <BR> &nbsp; 1. <A href="mailto:[email protected]">[email protected]</A></P>";

Is there a way I could just take that HTML and convert it to a .NET equivalent, preferably without changing the format?

thanks in advance.

A: 

Try this:

string s = "<P>Notes:&nbsp;&nbsp; Mails: <BR> &nbsp; 1. <A href='mailto:[email protected]'>[email protected]</A></P>";

You can not use the double quotes inside the string without escaping them. Or use the version I posted.

Nick
+3  A: 

Escape the quotes:

string s =  "<P>Notes:&nbsp;&nbsp; Mails: <BR> &nbsp; 1. <A href=\"mailto:[email protected]\">[email protected]</A></P>";
Chuck Conway
A: 
string s =  "<P>Notes:&nbsp;&nbsp; Mails: <BR> &nbsp; 1. <A href=\"mailto:[email protected]\">[email protected]</A></P>";

You have to escape the quotes with a slash "\".

Greg
Thank you. it works
mawa
A: 

There's no string literal in C# where you don't have to escape double quotes.

var str1 = "<A href=\"mailto:[email protected]\">";
var str2 = @"<A href=""mailto:[email protected]"">";
Jerome
+1  A: 

It looks like you just have to escape the quotes:

 string s =  "<P>Notes:&nbsp;&nbsp; Mails: <BR> &nbsp; 1. <A href=\"mailto:[email protected]\">[email protected]</A></P>";

In C# " denotes the beginning or end of a string, to use a " inside a string you need to 'escape' it like this \". You may also use verbatim string literals of the format

string s = @"this is a string with "" <-- a quote inside";
Albin Sunnanbo
A: 
string s = "<P>Notes:&nbsp;&nbsp;Mails:<br />&nbsp;1.<A href=\"mailto:[email protected]\">[email protected]</A></P>";
Andy Evans
A: 

You have several options:

For compile time support, you can escape quotes with \", as demonstrated so many times by others (so I'll not repeat), you can use a literal string @"..." where you can escape quotes with "", or you can paste your string in a resource file and let Visual Studio handle the escaping for you.

Alternately, you can often get by if you replace the double quote for a single; however, this can sometimes be a problem if your string contains JavaScript, as having two types of quotes at hand may be a necessary evil.

At run time you can avoid escaping by reading the text from a file, database, or some other resource; however, I get the impression this is not your intent.

kbrimington