The return of CopySourceAsHtml

In a recent post I did a walk through of using the Google syntax highlighter. In the end I got it to work. As also the comments pointed out there are two major drawbacks.

  1. The syntax highligher requires some scripts. These are installed on the codebetter site but a post read from an RSS feed will not be able to reach the scripts. The code will be an unreadable jumble.
  2. To work with html, xml or anything else with angle brackets requires using textarea tags. Many a blog post editor (but not all) will  encode the contents of the text enclosed. (> is translated into >). The result will be that the code is also jumbled, even when the scripts are reachable.

The comments also pointed to an old friend, CopySourceAsHtml. I had lost it when moving to VS 2008. The comments  pointed to a recipe to get it working in VS 2008. But that’s no longer needed, a VS 2008 version is now available.

The good thing with CopySourceAsHtml is that it works with any code file in VS, the Copy As Html item is always in the main edit menu.

For plain code

namespace Datema.ENCtracking.Repositories.Helpers


    public class RepositoryBase


        public RepositoryBase(string connectionString)


            connection.ConnectionString = connectionString;



For Html

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <style type="text/css">

        html,body, form


            height: 100%;




    <script type="text/javascript">

        function pageLoad() {

            window.onerror = tellerror;



        function tellerror(msg, url, linenumber) {

            if (msg.toString().indexOf("Image") >= 0 && msg.toString().indexOf("4001") >= 0) {

            return true;


            else {

                alert(‘Error message= ‘ + msg + ‘\nURL= ‘ + url + ‘\nLineNumber= ‘ + linenumber)

                return true;



For Xaml

<Grid x:Name="LayoutRoot" Background="White">

    <DeepEarth:Map x:Name="map" Margin="0,0,0,0" BorderThickness="10">



    <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10" >

        <Button x:Name="ButtonPortLabels" Content="Ports" Click="ButtonPortLabels_Click" FontSize="7" Opacity="0.5"/>

        <Slider x:Name="SliderZoom" Orientation="Vertical" Value="1" Height="200" Minimum="1" Maximum="20" ValueChanged="Slider_ValueChanged"></Slider>


All of these snippets where a matter of select, copy, paste. A thing to watch is the font size. Bu default CSAH takes the size of your editor. In my case the result will look far to big. Selecting a different size is no problem:

CSAH will remember  this setting.

No hassles and it looks good on the site and in a feedreader.

Welcome back !

This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Greg

    You could also check out Highlight at Its been around for quite a while and will format many different languages in to many different formats.

  • Kelly

    Pingback from