views:

201

answers:

3

I want to assign a xml code into a string variable. I can do this without escaping single or double-quotes by using triple-quote in python. Is there a similar way to do this in F# or C#?

+1  A: 

In C# the syntax is @"some string"

see here

shoosh
Looks more like a triple-quoted raw string.
Ignacio Vazquez-Abrams
You still need to encode `"` in the string, so this isn't really an equivalent of the Python's `"""` syntax.
Tomas Petricek
A: 

As shoosh said, you want to use the verbatim string literals in C#, where the string starts with @ and is enclosed in double quotation marks. The only exception is if you need to put a double quotation mark in the string, in which case you need to double it

System.Console.WriteLine(@"Hello ""big"" world");

would output

Hello "big" world

http://msdn.microsoft.com/en-us/library/362314fe.aspx

Sam
+4  A: 

As far as I know, there is no syntax corresponding to this in C# / F#. If you use @"str" then you have to replace quote with two quotes and if you just use "str" then you need to add backslash.

In any case, there is some encoding of ":

var str = @"allows
  multiline, but still need to encode "" as two chars";
var str = "need to use backslahs \" here";

However, the best thing to do when you need to embed large strings (such as XML data) into your application is probably to use .NET resources (or store the data somewhere else, depending on your application). Embedding large string literals in program is generally not very recommended. Also, there used to be a plugin for pasting XML as a tree that constructs XElement objects for C#, but I'm not sure whether it still exists.

Although, I would personally vote to add """ as known from Python to F# - it is very useful, especially for interactive scripting.

Tomas Petricek
Thnx Tomas. I just want to manage many "short" xml files easily by embeding. I think i should use singe-quote for the XML attributes. BTW, I read your book. It's great.
tk
@tk: Thanks! Yeah, I think that embedding strings isn't as "evil" as the guidelines say...
Tomas Petricek