1

So I have this piece of text that I need to be on a string so I can later add to a text file and should be like this string

<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
<requiredRuntime version="v4.0.20506" />
</startup>

I've tried to verbate it like

@"""<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    <requiredRuntime version="v4.0.20506" />
    </startup>"""

and also tried to work with concatenatio but I can't see to figure out how to include every quote to be on that string.

juharr
  • 31,034
  • 4
  • 52
  • 91
Mr.Toxy
  • 347
  • 3
  • 16

4 Answers4

4

Double quotes escape a single quote within @"":

string Text = @"<startup useLegacyV2RuntimeActivationPolicy=""true"">
    <supportedRuntime version=""v4.0"" sku="".NETFramework,Version=v4.0""/>
    <requiredRuntime version=""v4.0.20506"" />
  </startup>";
Alex K.
  • 165,803
  • 30
  • 257
  • 277
  • I've tried to do it as you mentioned but didnt worked for me. Instead I did as follows: "\r\n\r\n\r\n" – Mr.Toxy Apr 05 '16 at 10:08
1

Handy tool here http://www.freeformatter.com/java-dotnet-escape.html

Input the string and it will escape it for you.

"<startup useLegacyV2RuntimeActivationPolicy=\"true\">\r\n    <supportedRuntime version=\"v4.0\" sku=\".NETFramework,Version=v4.0\"/>\r\n    <requiredRuntime version=\"v4.0.20506\" />\r\n  </startup>"
CathalMF
  • 9,077
  • 5
  • 61
  • 89
0
private static void Main(string[] args)
        {
            string value =
                @"<startup useLegacyV2RuntimeActivationPolicy=""true""> <supportedRuntime version=""v4.0"" sku="".NETFramework,Version=v4.0""/>
                 <requiredRuntime version=""v4.0.20506"" /></startup>";
            Console.WriteLine(value);
            Console.Read();
        }
Anwar Ul-Haq
  • 1,786
  • 1
  • 13
  • 26
0

You can preface the initial double-quote of a string with the '@' character to handle escaping it :

var startupTag = @"<startup useLegacyV2RuntimeActivationPolicy=""true"">
                           <supportedRuntime version=""v4.0"" sku="".NETFramework,Version=v4.0""/>
                           <requiredRuntime version=""v4.0.20506"" />
                   </startup>";

You can see a live example of this here.

Rion Williams
  • 72,294
  • 36
  • 192
  • 318