in

ASP.NET Weblogs

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""?>
<soap:Envelope
 xmlns:xsi=""
http://www.w3.org/2001/XMLSchema-instance""
 xmlns:xsd=""
http://www.w3.org/2001/XMLSchema""
 xmlns:soap=""
http://schemas.xmlsoap.org/soap/envelope/"">
 <soap:Body>
  <ForceBuild xmlns=""
http://tempuri.org/"">
   <projectName><![CDATA[{0}]]></projectName>
  </ForceBuild>
 </soap:Body>
</soap:Envelope>";

Now I know.

Comments

 

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.

http://research.microsoft.com/comega/doc/comega_tutorial_content_classes.htm">http://research.microsoft.com/comega/doc/comega_tutorial_content_classes.htm

http://research.microsoft.com/comega/

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();
sw.Write(streamstring);
sw.Close();
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
 

j said:

gd point

May 4, 2008 4:35 PM
 

cosmin said:

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

May 18, 2008 2:36 PM
 

KooK said:

Thanks very much

July 19, 2008 4:37 AM
 

Owen said:

Thanks! You're still saving random developers a lot of hair-pulling via google ;)

August 26, 2008 4:28 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
 

Shiva said:

On further investigation, it seems like it isn't the ; that is the problem. It is that whether I use \" or "" to include the literal quote character, it is not working. My environment is VS2005 Standard on Win XP.

< a href="bytes.com/.../thread225840.html">Another person in following thread is having same issue.</a>

:(

September 18, 2008 1:58 PM
 

Sugarbomb said:

Uh, yeah. I knew that!

Thanks!

October 16, 2008 12:51 AM
 

al said:

thx!

November 5, 2008 4:33 PM
 

Berry said:

Thanks.  This does work, but not under all circumstances.

I'm having issues constructing a SQL string from variables.  I have to put double quotes around each value, and the fact that I'm piecing together a string from parts appears to be preventing me from using either the backslash or the double-double quote.  

Neither approach works when using + to concatenate strings together.

December 2, 2008 3:18 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

Leave a Comment

(required)  
(optional)
(required)  
Add