1

I am generating dynamic HTML from c# application, By using string builder the html tags are appended and finally forming full html. In all the stringbuilder i am replacing html placeholders with c# object.

The html which am building is complex one, i need to fill up the placeholder from c# properties and sometimes database call. Am not having XML so not using XSLT. Since my html file huge and lot of string buider is required. Most of the sections are repeated based on business logic.

Everything is working fine now, i want to move away hardedcoded html string from stringbuilder, because maintenence will tough later on. Any best suggestions to get rid of hardcoded html from console application.

Performance wise, is it good to use lot of string builder( atleast 500 stringbuilder am using) For example, my html split is as below

  1. header part
  2. Body part
    2.1 section1
    2.1.1 section 1.1
    2.1.2 section 1.2
    2.1.3 section 1.3
    2.2 section2
    2.3 section3
    2.4 section4
    2.5 section5
    3.Footer Part
  3. Temrs and conditions

I have just provided few lines of code here for your reference.

Code Sample:

    htmlStringBuilder.Append("<table class=\"paddingIndendation\" style=\"width: 100%;\" border=\"1\">");
    htmlStringBuilder.Append(string.Format("<tr><td>Location {0}</td><td>:</td><td>{1}</td></tr>", location.LocationNumber, location.AddressLine1));
    htmlStringBuilder.Append(string.Format("<tr><td colspan=\"2\" style=\"text-align:right;\"></td><td>{0}</td></tr>", location.AddressLine2));
    htmlStringBuilder.Append(string.Format("<tr><td colspan=\"2\" style=\"text-align:right;\"></td><td>{0} {1}</td></tr>", location.PostalCode, location.City));
htmlStringBuilder.Append(string.Format("<tr><td colspan=\"2\" style=\"text-align:right;\"></td><td>{0} {1}</td></tr>", this.Id,this.StartEffectiveDate));

I want to move the hardcorded html content to somewhere(May be to file or resource file) and replace the placeholders with properties. Can anyone advise best way to do it.

2
  • 1
    why don't you just create pages in the long run it's gonna save you a lot of head ache and maintenance nightmares ..you can maintain a lot of what you're doing in .aspx markup..
    – MethodMan
    Nov 24, 2015 at 1:56
  • the html which am generating is used to create PDF document. Creating pages in the long run mean? i am not clear on this. Could you please explain.
    – Govind
    Nov 24, 2015 at 6:25

2 Answers 2

9

If you look at the Maintainence prespective you can actually make a HTML file with placeholders like this

<table class="paddingIndendation" style="width: 100%;" border="1">
    <tr>
        <td>Location ~LocationNumber~</td>
        <td>:</td>
        <td>~AddressLine1~</td>
    </tr>
    <tr>
        <td colspan="2" style="text-align:right;"></td>
        <td>~AddressLine2~</td>
    </tr>
    <tr>
        <td colspan="2" style="text-align:right;"></td>
        <td>~PostalCode~ ~City~</td>
    </tr>

Load the string in a string variable like

string htmlstring = File.ReadAllText("yourhtml.txt");

And then probably you can create a small function where you can replace all these placeholders with whatever properties or database call like this

htmlstring = htmlstring.Replace("~LocationNumber~",location.LocationNumber);
htmlstring = htmlstring.Replace("~AddressLine1~",location.AddressLine1);
htmlstring = htmlstring.Replace("~AddressLine2~",location.AddressLine2);    
htmlstring = htmlstring.Replace("~PostalCode~",location.PostalCode);
htmlstring = htmlstring.Replace("~City~",location.City);
2

I'm not sure why the question is getting downvoted.

The commenter is right; you're asking for a LOT of trouble doing this, whether it performs well or not. So StringBuilder is really the wrong way to go. If you're going to do it anyway, then it doesn't matter whether it performs well. You'll have traded away much better ways to do it, in favor of keeping it simple enough for you to maintain. The performance will be whatever the performance is.

What I would do, if I were you, and it is still a lot of work, would be to create a local, private website, using ASP.NET (MVC if you want), and then use scripts to retrieve or spider your web site, pulling down all the generated HTML you want.

2
  • the html which am generating is used to create PDF by using external PDF
    – Govind
    Nov 24, 2015 at 7:32
  • Okay. I don't think my answer depends on what you are using it for. My assumption is that you are assembling HTML the way you are because some of the HTML has to be dynamically inserted into most of the HTML which is fairly static. This is precisely what ASP.NET does and has amazing support for -- StringBuilder does not. It's just that, to get at the final HTML, you must issue a Web-based request. PowerShell 3 and later has a very easy Invoke-WebRequest command; there are other easy ways.
    – Alan McBee
    Nov 24, 2015 at 16:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.