Here’s a useful bit of C# you can paste into your LINQPad statements to quickly hack together some HTML (could be useful for quick ad-hoc database reports):

Action<string, string> HtmlToClipboard = delegate( string htmlFragment, string title )
{
    Func<int, string> To8DigitString = delegate(int x) { return String.Format("{0,8}", x); };

    if (title == null) title = "From Clipboard"; 

    var sb = new System.Text.StringBuilder();
    var header = @"Format:HTML Format
Version:1.0
StartHTML:<<<<<<<1
EndHTML:<<<<<<<2
StartFragment:<<<<<<<3
EndFragment:<<<<<<<4
StartSelection:<<<<<<<3
EndSelection:<<<<<<<3
";

    var pre = @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">
<HTML><HEAD><TITLE>" + title + @"</TITLE></HEAD><BODY><!--StartFragment-->";

    var post = @"<!--EndFragment--></BODY></HTML>";

    sb.Append(header);
    int startHTML = sb.Length;

    sb.Append(pre);
    int fragmentStart = sb.Length;

    sb.Append(htmlFragment);
    int fragmentEnd = sb.Length;

    sb.Append(post);
    int endHTML = sb.Length;

    sb.Replace("<<<<<<<1", To8DigitString(startHTML));
    sb.Replace("<<<<<<<2", To8DigitString(endHTML));
    sb.Replace("<<<<<<<3", To8DigitString(fragmentStart));
    sb.Replace("<<<<<<<4", To8DigitString(fragmentEnd));

    string data = sb.ToString();
    System.Windows.Forms.Clipboard.Clear();
    System.Windows.Forms.Clipboard.SetText(data, System.Windows.Forms.TextDataFormat.Html);
};

Note you’ll need to add a reference in LINQPad (Query -> Advanced properties) to System.Windows.Forms.dll (it uses the Clipboard class). For more detail on why it jumps through so many hoops, read this post by Mike Stall.

About these ads