HTML within a StringBuilder

0

I have HTML where I am formatting it inside a StringBuilder , where there are "quotation marks" I am passing a slash, but it is not opening the correct HTML.

public string CorpoPaginaHtml()
        {
            StringBuilder corpoemail = new StringBuilder();
            corpoemail.AppendLine("");
            corpoemail.AppendLine("< !DOCTYPE html >");
            corpoemail.AppendLine("< html lang = \"pt_BR\"> ");
            corpoemail.AppendLine("< head > ");
            corpoemail.AppendLine("< meta charset = \"utf-8\" >");
            corpoemail.AppendLine("< meta http - equiv = \"X-UA-Compatible\" content = \"IE=edge\" >");
            corpoemail.AppendLine("< meta name = \"viewport\" content = \"width=device-width, initial-scale=1\" >");
            corpoemail.AppendLine("< title > Aviso de Produto</ title >");
            corpoemail.AppendLine("< link rel = \"stylesheet\" href = \"http://netdna.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css\" > ");
            corpoemail.AppendLine("< script src = \"http://imsky.github.com/holder/holder.js\" ></ script > ");
            corpoemail.AppendLine("</ head >");
            corpoemail.AppendLine("< body >");
            corpoemail.AppendLine(" < div class=\"container\" >");
            corpoemail.AppendLine("< hr />");
            corpoemail.AppendLine("<h2>Aviso de Produto</h2>");
            corpoemail.AppendLine("< div class=\"row\">");
            corpoemail.AppendLine("<div id = \"items -list\" class=\"col-xs-8\">");
            corpoemail.AppendLine("<div class=\"media\">");
            corpoemail.AppendLine("<a class=\"media -left\" href=\"#\">");
            corpoemail.AppendLine("<img alt = \"64x64\" width=\"150\" height=\"100\"  src=\"http://media.webdevacademy.com.br/2014/02/placeholder.jpg\">");
            corpoemail.AppendLine("</a>");
            corpoemail.AppendLine("< div class=\"media -body\">");
            corpoemail.AppendLine(" <h4 class=\"media -heading\">Titulo</h4>");
            corpoemail.AppendLine(" Cras sit amet nibh libero, in gravida nulla.Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis.Fusce condimentum nunc ac nisi vulputate fringilla.Donec lacinia congue felis in faucibus.");
            corpoemail.AppendLine("</div>");
            corpoemail.AppendLine("</div>");
            corpoemail.AppendLine(" </div>");
            corpoemail.AppendLine("</div>");
            corpoemail.AppendLine("<hr />");
            corpoemail.AppendLine("< footer class\"footer -inverse\">");
            corpoemail.AppendLine(" <div class=\"container\">");
            corpoemail.AppendLine("<p class=\"text -muted\">&copy;2017 - Modelo Exemplo Onofre.</p>");
            corpoemail.AppendLine(" </div>");
            corpoemail.AppendLine(" </footer>");
            corpoemail.AppendLine(" <script src = \"https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js\" ></ script > ");
            corpoemail.AppendLine(" <script src = \"http://netdna.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js\" ></ script > ");
            corpoemail.AppendLine(" </ body >");
            corpoemail.AppendLine("</ html >");

            return corpoemail.ToString(); 
        }
    
asked by anonymous 21.03.2017 / 18:44

2 answers

3

You do not need this complication and waste of whole processing, it can be simple like this:

public class Program {
    public static void Main()   {
        var html = @"<!DOCTYPE html>
        <html lang = ""pt_BR""> 
        <head> 
        <meta charset = ""utf-8"">
        <meta http-equiv = ""X-UA-Compatible"" content = ""IE=edge"">
        <meta name = ""viewport"" content = ""width=device-width, initial-scale=1"">
        <title> Aviso de Produto</title>
        <link rel = ""stylesheet"" href = ""http://netdna.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css""> 
        <script src = ""http://imsky.github.com/holder/holder.js""></script> 
        </head>
        <body>
         <div class=""container"">
        <hr/>
        <h2>Aviso de Produto</h2>
        <div class=""row"">
        <div id = ""items-list"" class=""col-xs-8"">
        <div class=""media"">
        <a class=""media -left"" href=""#"">
        <img alt = ""64x64"" width=""150"" height=""100""  src=""http://media.webdevacademy.com.br/2014/02/placeholder.jpg"">
        </a>
        <div class=""media-body"">
         <h4 class=""media-heading"">Titulo</h4>
         Cras sit amet nibh libero, in gravida nulla.Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis.Fusce condimentum nunc ac nisi vulputate fringilla.Donec lacinia congue felis in faucibus.
        </div>
        </div>
         </div>
        </div>
        <hr/>
        <footer class""footer-inverse"">
         <div class=""container"">
        <p class=""text-muted"">&copy;2017 - Modelo Exemplo Onofre.</p>
         </div>
         </footer>
         <script src = ""https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js""></script> 
         <script src = ""http://netdna.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js""></script> 
         </body>
        </html>";
        System.Console.WriteLine(html);
    }
}

See working on .NET Fiddle . And at Coding Ground . Also put it on GitHub for future reference .

The code is full of errors, apparently I have corrected all of them.

Even if you insist on using StringBuilder at least start the capacity of an estimated text size, I know, 512, 1024 to improve performance and put less pressure on the GC.

    
21.03.2017 / 19:01
1

First, tags can not have space between the < , > signs and the tag name, and this is done on almost all lines.

Second, there are other places with spaces like in meta http-equiv that can not.

Third problem,% w / o% is missing the assignment signal.

Review all your HTML, here is a base, you can see the result, but there are still errors.

public string CorpoPaginaHtml()
{
    StringBuilder corpoemail = new StringBuilder();
    corpoemail.AppendLine("");
    corpoemail.AppendLine("<!DOCTYPE html>");
    corpoemail.AppendLine("<html lang=\"pt_BR\"> ");
    corpoemail.AppendLine("<head> ");
    corpoemail.AppendLine("<meta charset=\"utf-8\">");
    corpoemail.AppendLine("<meta http-equiv = \"X-UA-Compatible\" content = \"IE=edge\">");
    corpoemail.AppendLine("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
    corpoemail.AppendLine("<title>Aviso de Produto</title>");
    corpoemail.AppendLine("<link rel=\"stylesheet\" href=\"http://netdna.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css\"> ");
    corpoemail.AppendLine("<script src = \"http://imsky.github.com/holder/holder.js\"></script> ");
    corpoemail.AppendLine("</head>");
    corpoemail.AppendLine("<body>");
    corpoemail.AppendLine(" <div class=\"container\">");
    corpoemail.AppendLine("<hr/>");
    corpoemail.AppendLine("<h2>Aviso de Produto</h2>");
    corpoemail.AppendLine("<div class=\"row\">");
    corpoemail.AppendLine("<div id = \"items-list\" class=\"col-xs-8\">");
    corpoemail.AppendLine("<div class=\"media\">");
    corpoemail.AppendLine("<a class=\"media-left\" href=\"#\">");
    corpoemail.AppendLine("<img alt=\"64x64\" width=\"150\" height=\"100\"  src=\"http://media.webdevacademy.com.br/2014/02/placeholder.jpg\">");
    corpoemail.AppendLine("</a>");
    corpoemail.AppendLine("<div class=\"media-body\">");
    corpoemail.AppendLine(" <h4 class=\"media-heading\">Titulo</h4>");
    corpoemail.AppendLine(" Cras sit amet nibh libero, in gravida nulla.Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis.Fusce condimentum nunc ac nisi vulputate fringilla.Donec lacinia congue felis in faucibus.");
    corpoemail.AppendLine("</div>");
    corpoemail.AppendLine("</div>");
    corpoemail.AppendLine(" </div>");
    corpoemail.AppendLine("</div>");
    corpoemail.AppendLine("<hr/>");
    corpoemail.AppendLine("<footer class=\"footer -inverse\">");
    corpoemail.AppendLine(" <div class=\"container\">");
    corpoemail.AppendLine("<p class=\"text -muted\">&copy;2017 - Modelo Exemplo Onofre.</p>");
    corpoemail.AppendLine(" </div>");
    corpoemail.AppendLine(" </footer>");
    corpoemail.AppendLine(" <script src = \"https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js\"></script> ");
    corpoemail.AppendLine(" <script src = \"http://netdna.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js\"></script> ");
    corpoemail.AppendLine(" </body>");
    corpoemail.AppendLine("</html>");

    return corpoemail.ToString(); 
}
    
21.03.2017 / 18:58