Loren Halvorson's Blog

If your only tool is a hammer...

C# 101 - Representing a double quote in string literals

I'm sure almost every C# developer already knew this, but I thought a post might help the few that didn't. I had always wondered how it was done and stumbled across it yesterday buried in an example in the C# Language Specification.

If you want to represent a double quote in a string literal, the escape sequence is two double quotes.

string myString = @"<xml attribute=""my attribute""/>";

I have found this useful for storing nicely formatted XML fragments in constants without resorting to 1) putting it all on one long line without string literals or 2) loading from a file or resource or 3) concatenating at run time, or 4) switching to single quotes.

  private const string requestXml =
@"<?xml version=""1.0"" encoding=""utf-8""?>
  <ForceBuild xmlns=""

Now I know.


Eron Wright said:

Then there is the COmega language from Microsoft Research that permits embedding XML directly in the source code - with strong typing. SQL statements and XQuery too. Exciting stuff.



That said, yay for multi-line strings!
# October 20, 2004 10:07 PM

andrew said:

another method would be using \ ... for example if you wanted to strip <?xml version="1.0" encoding="utf-8"?> out of your newly transformed file (xsl) ... you could do this:

StreamReader fsnew = new StreamReader(filenamenew,Encoding.UTF8);
string streamstring = fsnew.ReadToEnd();

streamstring = streamstring.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");

FileInfo fi = new FileInfo(filenamenew);
StreamWriter sw = fi.CreateText();
fi = null;
sw = null;
# October 22, 2004 1:54 PM

doaa said:

tank you very much.actually i am one of the few who didnt know that issue.

# September 23, 2006 2:31 AM

Ice said:

Thanks for posting. This actually helped.

# May 4, 2007 4:18 PM

pemberai said:

thanks a lot for this code

# July 1, 2007 8:25 AM

Lebza said:

Thanx a lot....i needed a quick and u came to rescue

# August 2, 2007 6:55 AM

Mark said:

groovy.  I'm glad you had the guts to ask.  I had the same question.

# November 20, 2007 5:35 PM

hesadi- sri lanka said:

hey , this is cool. keeep up the good work..

# January 24, 2008 4:58 AM

cosmin said:

that was helpful..just got into this trouble and found a quick answer...thanks

# May 18, 2008 2:36 PM

Shiva said:

This (double quote solution) does not seem to work if I have a semi-colon ; in my string that needs to be wrapped around quotes, for example, in my text driver connection string I need to wrap extended properties around double quotes.

           connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + importFolder + ";"

                               + "Extended Properties='text;HDR=No;FMT=Delimited;'";

# September 18, 2008 11:53 AM

Sugarbomb said:

Uh, yeah. I knew that!


# October 16, 2008 12:51 AM

Sri said:

Thanks.It helped me.

# January 15, 2009 4:45 PM

joel said:

thanks!!! Really helped me out - needed a quick answer.

# February 16, 2009 8:33 AM

Mike Devenney said:

Wouldn't "\"" work?

# June 15, 2009 9:52 AM

PAVEL said:

yes "\""  is working

# June 23, 2009 11:16 AM

nripinbabu@msn.com said:

Thanks andrew !!!1

Your Tip Really Worked !!!1

# July 31, 2009 9:23 AM

Sri said:

very nice quick tip when I got stuck !!

# September 28, 2009 1:15 PM

Gideon said:

i added the @ infront and it also worked !

access_Sql = @"SELECT TOP 10 * FROM [GP GL Balances] WHERE ((([GP GL Balances].[OPICS GL]) Like ""10001*"";

# January 14, 2010 2:25 AM

Roop said:

It really helped me out.

"\"" Works fine

# February 16, 2010 3:37 PM