0

I am building a string of HTML. I want to have it multiline so that I can read it, but I can't seem to figure out how to escape the double quotes. I am using string = @"bla" for a string literal to allow for the multiline but when I can't seem to use a backslash to escape the double quotes.

Here is what I have...

string output;

output = @"<html>
    <head>
        <style>
            th, tr, td { padding-left: 5px; }
        </style>    
    </head>
    <body>
        <table>
            <tr>
                <td colspan=\"2\">";

It's getting hung up on the <td colspan=\"2\"> Can I not use \ with the literal? What are my options?

Dizzy49
  • 1,360
  • 24
  • 35
  • 1
    1. no harm in adding a + to concatenate it's still legible. 2. Replace the \ with another quote and use double quotes var output = @"
    ";
    – Train Mar 26 '22 at 00:39

2 Answers2

2

You just use 2 consecutive double-quotes (""):

string s = @"Porky The Pig said, ""That's all, Folks!""...";

But you might want to use a proper templating engine, for instance (but by no means limited to):

They'll almost certainly be less susceptible to various injection attacks than rolling your own.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
-2

You may find it more desirable to create HTML in an HTML file rather than concatenating strings. Try the following:

Create a Windows Forms App (.NET Framework)

VS 2019:

Open Solution Explorer

  • In VS menu, click View
  • Select Solution Explorer

Open Properties Window

  • In VS menu, click View
  • Select Properties Window

Add folder (name: Html)

  • In Solution Explorer, right-click <project name>
  • Select Add
  • Select New folder. Rename to desired name (ex: Html)

Add HTML file

  • In Solution Explorer, right-click the folder you created (ex: Html)
  • Select Add
  • Select New Item...
  • On left side, under Visual C# Items, click Web
  • Select HTML page (name: HTMLPage1.html)
  • Click Add
  • In Solution Explorer, click HTML page (ex: HTMLPage1.html)
  • In Properties Window, set the following property: Build Action: Embedded Resource

Create a class (name: HelperLoadResource.cs)

Add the following using statements:

  • using System;
  • using System.Text;
  • using System.IO;
  • using System.Reflection;

HelperLoadResource

Note: The following code is from this post.

public static class HelperLoadResource
{
    public static string ReadResource(string filename)
    {
        //use UTF8 encoding as the default encoding
        return ReadResource(filename, Encoding.UTF8);
    }

    public static string ReadResource(string filename, Encoding fileEncoding)
    {
        string fqResourceName = string.Empty;
        string result = string.Empty;

        //get executing assembly
        Assembly execAssembly = Assembly.GetExecutingAssembly();

        //get resource names
        string[] resourceNames = execAssembly.GetManifestResourceNames();

        if (resourceNames != null && resourceNames.Length > 0)
        {
            foreach (string rName in resourceNames)
            {
                if (rName.EndsWith(filename))
                {

                    //set value to 1st match
                    //if the same filename exists in different folders,
                    //the filename can be specified as <folder name>.<filename>
                    //or <namespace>.<folder name>.<filename>
                    fqResourceName = rName;

                    //exit loop
                    break;
                }
            }

            //if not found, throw exception
            if (String.IsNullOrEmpty(fqResourceName))
            {
                throw new Exception($"Resource '{filename}' not found.");
            }

            //get file text
            using (Stream s = execAssembly.GetManifestResourceStream(fqResourceName))
            {
                using (StreamReader reader = new StreamReader(s, fileEncoding))
                {
                    //get text
                    result = reader.ReadToEnd();
                }
            }
        }

        return result;
    }
}

Usage:

//get HTML as string
string html = HelperLoadResource.ReadResource("HTMLPage1.html");

Resources:

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24