Mr.Toxy
Mr.Toxy

Reputation: 367

How do I maintain multiple quotes quoted in a string in c#

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.

Upvotes: 1

Views: 319

Answers (4)

Rion Williams
Rion Williams

Reputation: 76557

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.

Upvotes: 0

Anwar Ul-haq
Anwar Ul-haq

Reputation: 1881

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();
        }

Upvotes: 0

CathalMF
CathalMF

Reputation: 10055

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>"

Upvotes: 1

Alex K.
Alex K.

Reputation: 175826

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>";

Upvotes: 4

Related Questions