Techniques for building HTML in code <p>An html template is compiled into the application as a resource. A <strong>fragment</strong> of the HTML template looks like:</p> <pre><code>&lt;A href="%PANELLINK%" target="_blank"&gt; &lt;IMG border="0" src="%PANELIMAGE%" style="%IMAGESTYLE%"&gt; &lt;/A&gt;&lt;BR&gt; %CAPTIONTEXT% </code></pre> <p>i like it like this because the larger resource HTML file contains styling, no-quirks mode, etc.</p> <p>But as is always the case, they now want the option that the Anchor tag should be omitted if there is no link. Also if there is no caption, then the BR tag should be omitted.</p> <hr> <h2>Considered Technique Nº1</h2> <p>Given that i don't want to have to build entire HTML fragments in C# code, i <em>considered</em> something like:</p> <pre><code>%ANCHORSTARTTAGPREFIX%&lt;A href="%PANELLINK%" target="_blank"&gt;%ANCHORSTARTTAGPOSTFIX% &lt;IMG border="0" src="%PANELIMAGE%" style="%IMAGESTYLE%"&gt; %ANCHORENDTAGPREFIX%&lt;/A&gt;%ANCHORENDTAGPOSTFIX%CAPTIONPREFIX%&lt;BR&gt; %CAPTIONTEXT%%CAPTIONPOSTFIX% </code></pre> <p>with the idea that i could use the pre and postfixes to turn the HTML code into:</p> <pre><code>&lt;!--&lt;A href="%PANELLINK%" target="_blank"&gt;--&gt; &lt;IMG border="0" src="%PANELIMAGE%" style="%IMAGESTYLE%"&gt; &lt;!--&lt;/A&gt;--&gt;&lt;!--&lt;BR&gt; %CAPTIONTEXT%--&gt; </code></pre> <p>But that is just rediculous, plus one answerer reminds us that it wastes bandwith, and can be buggy.</p> <hr> <h2>Considered Technique Nº2</h2> <p>Wholesale replacement of tags:</p> <pre><code>%AnchorStartTag% &lt;IMG border="0" src="%PANELIMAGE%" style="%IMAGESTYLE%"&gt; %AnchorEndTag%%CaptionStuff% </code></pre> <p>and doing a find-replace to change</p> <pre><code>%AnchorStartTag% </code></pre> <p>with</p> <pre><code>"&lt;A href=\"foo\" target=\"blank\"" </code></pre> <hr> <h2>Considered Technique Nº3</h2> <p>i considered giving an ID to the important HTML elements:</p> <pre><code>&lt;A id="anchor" href="%PANELLINK%" target="_blank"&gt; &lt;IMG border="0" src="%PANELIMAGE%" style="%IMAGESTYLE%"&gt; &lt;/A&gt;&lt;BR id="captionBreak"&gt; %CAPTIONTEXT% </code></pre> <p>and then using an HTML DOM parser to programatically delete nodes. But there is no easy access to a trustworthy HTML DOM parser. If the HTML was instead xhtml i would use various built-in/nativly available xml DOM parsers.</p> <hr> <h2>Considered Technique Nº4</h2> <p>What i actually have so far is:</p> <pre><code>private const String htmlEmptyTemplate = @"&lt;!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01//EN\"""+Environment.NewLine+ @" ""http://www.w3.org/TR/html4/strict.dtd""&gt;"+Environment.NewLine+ @"&lt;HTML&gt;"+Environment.NewLine+ @"&lt;HEAD&gt;"+Environment.NewLine+ @" &lt;TITLE&gt;New Document&lt;/TITLE&gt;"+Environment.NewLine+ @" &lt;META http-equiv=""X-UA-Compatible"" content=""IE=edge""&gt;"""+Environment.NewLine+ @" &lt;META http-equiv=""Content-Type"" content=""text/html; charset=UTF-8""&gt;"+Environment.NewLine+ @"&lt;/HEAD&gt;"+Environment.NewLine+ @""+Environment.NewLine+ @"&lt;BODY style=""margin: 0 auto""&gt;"+Environment.NewLine+ @" &lt;DIV style=""text-align:center;""&gt;"+Environment.NewLine+ @" %ContentArea%"+Environment.NewLine+ @" &lt;/DIV&gt;" + Environment.NewLine + @"&lt;/BODY&gt;" + Environment.NewLine + @"&lt;/HTML&gt;"; private const String htmlAnchorStartTag = @"&lt;A href=""%PANELLINK%"" target=""_blank""&gt;"; //Image is forbidden from having end tag private const String htmlImageTag = @"&lt;IMG border=""0"" src=""%PANELIMAGE%"" style=""%IMAGESTYLE%""&gt;"; private const String htmlCaptionArea = @"&lt;BR&gt;%CAPTIONTEXT%"; </code></pre> <p>And i already want to gouge my eyeballs out. Building HTML in code is a nightmare. It's a nightmare to write, a nightmare to debug, and a nightmare to maintain - and it will makes things difficult on the next guy. i'm hoping for another solution - since i <strong>am</strong> the next guy.</p>