HTML inside a Stringbuilder

Asked

Viewed 1,230 times

0

I have an HTML where I’m formatting it inside a StringBuilder, where there are "quotes" I am passing a bar, 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(); 
        }
  • What’s the problem? What do you mean "is not opening the correct HTML"?

  • < footer class\"footer -inverse\">" is missing the signal = e.g. I think Voce has to revise its HTML

  • 1

    Why use StringBuilder?

  • Have any answers solved what was in doubt? Do you need something else to be improved? Do you think it is possible to accept it now?

2 answers

3

You don’t need all this complication and waste processing, it can be as simple as that:

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

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

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

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

HTML correto

  • worked out, now how could I concatenate a variable within html? I appreciate the help

  • If it’s just one or a few variables just use the +, this will be turning into Contact() and will be efficient. The problem will only occur if you use a loop, make several concatenations in this way is bad, but it needs to be several. See http://answall.com/q/191264/101

1

First, tags cannot have space between signals <, > and the name of the tag and this is done on almost every line.

Second, it has other places with spaces as in meta http-equiv that can’t.

Third problem, in the footer class "footer -inverse" the assignment signal is missing.

Review all your HTML, here’s a base, you can see the result, but still have 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(); 
}
  • has a lot of errors his html... <div id = \"items -list\" class=\"col-xs-8\">" so I suggested he review the whole html ..

  • 1

    @andrepaulo Pronto

  • @jbueno, very grateful, I will review

  • @jbueno, it worked, now how could I concatenate a variable within html? thanks for the help

  • Uses normal string concatenation. @itasouza

Browser other questions tagged

You are not signed in. Login or sign up in order to post.