<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>out &#62;&#62; m_Conscientia; &#187; .NET</title>
	<atom:link href="http://blog.hypercomplex.co.uk/index.php/tag/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hypercomplex.co.uk</link>
	<description>a multidimensional braindump</description>
	<lastBuildDate>Mon, 02 Aug 2010 22:30:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>P/Invoke Interop Assistant</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2010/05/pinvoke-interop-assistant/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2010/05/pinvoke-interop-assistant/#comments</comments>
		<pubDate>Sat, 08 May 2010 23:45:28 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[interop]]></category>
		<category><![CDATA[p/invoke]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=900</guid>
		<description><![CDATA[After messing about for literally hours trying to p/invoke GetTokenInformation only for it to return what appeared to be sinographs, I came accross the P/Invoke interop assistant on CodePlex. Although this didn&#8217;t solve the my problem (which was correctly marshalling a fixed size char array), it would doubtlessly have saved me time writing the boiler [...]]]></description>
			<content:encoded><![CDATA[<p>After messing about for literally hours trying to p/invoke <a href="http://msdn.microsoft.com/en-us/library/aa446671%28VS.85%29.aspx">GetTokenInformation</a> only for it to return what appeared to be sinographs, I came accross the <a href="http://clrinterop.codeplex.com/releases/view/14120">P/Invoke interop assistant</a> on CodePlex.</p>
<div id="attachment_901" class="wp-caption aligncenter" style="width: 460px"><a href="http://blog.hypercomplex.co.uk/wp-content/uploads/2010/05/pinvoke.jpg"><img src="http://blog.hypercomplex.co.uk/wp-content/uploads/2010/05/pinvoke.jpg" alt="" title="pinvoke assistant" width="450" height="258" class="size-medium wp-image-901" /></a><p class="wp-caption-text">The P/Invoke Interop Assistant: it generates some useful stuff</p></div>
<p>Although this didn&#8217;t solve the my problem (which was correctly marshalling a fixed size char array), it would doubtlessly have saved me time writing the boiler plate structs, enums and method signatures. Highly recommended.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2010/05/pinvoke-interop-assistant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parsing HTML tables into System.Data.DataTable</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2010/05/parsing-html-tables-into-system-data-datatable/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2010/05/parsing-html-tables-into-system-data-datatable/#comments</comments>
		<pubDate>Thu, 06 May 2010 20:50:08 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DataSet]]></category>
		<category><![CDATA[DataTable]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[RegEx]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=877</guid>
		<description><![CDATA[What follows is a quick and dirty class I made to parse HTML tables into DataTables. As usual, it is the result of internet search/run/bug fix/refactor. In use, it looks little like this: WebClient client = new WebClient&#40;&#41;; string html = client.DownloadString&#40;@&#34;http://www.table.co.uk&#34;&#41;; DataSet dataSet = HtmlTableParser.Parse&#40;html&#41;; Here is the implementation. It&#8217;s not optimised for runtime [...]]]></description>
			<content:encoded><![CDATA[<p>What follows is a quick and dirty class I made to parse HTML tables into <a href="http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx">DataTable</a>s. As usual, it is the result of internet search/run/bug fix/refactor.</p>
<p>In use, it looks little like this:</p>

<div class="wp_codebox"><table><tr id="p8776"><td class="code" id="p877code6"><pre class="csharp" style="font-family:monospace;">WebClient client <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> WebClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">string</span> html <span style="color: #008000;">=</span> client<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;http://www.table.co.uk&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
DataSet dataSet <span style="color: #008000;">=</span> HtmlTableParser<span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>html<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Here is the implementation. It&#8217;s not optimised for runtime performance, but it works.</p>

<div class="wp_codebox"><table><tr id="p8777"><td class="code" id="p877code7"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// HtmlTableParser parses the contents of an html string into a System.Data DataSet or DataTable.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> HtmlTableParser
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> RegexOptions ExpressionOptions <span style="color: #008000;">=</span> RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">Singleline</span> <span style="color: #008000;">|</span> RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">Multiline</span> <span style="color: #008000;">|</span> RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">IgnoreCase</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> CommentPattern <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;!--(.*?)--&gt;&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> TablePattern <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;table[^&gt;]*&gt;(.*?)&lt;/table&gt;&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> HeaderPattern <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;th[^&gt;]*&gt;(.*?)&lt;/th&gt;&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> RowPattern <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;tr[^&gt;]*&gt;(.*?)&lt;/tr&gt;&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> CellPattern <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;td[^&gt;]*&gt;(.*?)&lt;/td&gt;&quot;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Given an HTML string containing n table tables, parse them into a DataSet containing n DataTables.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;html&quot;&gt;An HTML string containing n HTML tables&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;A DataSet containing a DataTable for each HTML table in the input HTML&lt;/returns&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> DataSet ParseDataSet<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> html<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        DataSet dataSet <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DataSet<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        MatchCollection tableMatches <span style="color: #008000;">=</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Matches</span><span style="color: #008000;">&#40;</span>
            WithoutComments<span style="color: #008000;">&#40;</span>html<span style="color: #008000;">&#41;</span>,
            TablePattern,
            ExpressionOptions<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Match tableMatch <span style="color: #0600FF; font-weight: bold;">in</span> tableMatches<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            dataSet<span style="color: #008000;">.</span><span style="color: #0000FF;">Tables</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>ParseTable<span style="color: #008000;">&#40;</span>tableMatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> dataSet<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Given an HTML string containing a single table, parse that table to form a DataTable.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;tableHtml&quot;&gt;An HTML string containing a single HTML table&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;A DataTable which matches the input HTML table&lt;/returns&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> DataTable ParseTable<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> tableHtml<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> tableHtmlWithoutComments <span style="color: #008000;">=</span> WithoutComments<span style="color: #008000;">&#40;</span>tableHtml<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        DataTable dataTable <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DataTable<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        MatchCollection rowMatches <span style="color: #008000;">=</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Matches</span><span style="color: #008000;">&#40;</span>
            tableHtmlWithoutComments,
            RowPattern,
            ExpressionOptions<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        dataTable<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AddRange</span><span style="color: #008000;">&#40;</span>tableHtmlWithoutComments<span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;th&quot;</span><span style="color: #008000;">&#41;</span>
                                       <span style="color: #008000;">?</span> ParseColumns<span style="color: #008000;">&#40;</span>tableHtml<span style="color: #008000;">&#41;</span>
                                       <span style="color: #008000;">:</span> GenerateColumns<span style="color: #008000;">&#40;</span>rowMatches<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        ParseRows<span style="color: #008000;">&#40;</span>rowMatches, dataTable<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> dataTable<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Strip comments from an HTML stirng</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;html&quot;&gt;An HTML string potentially containing comments&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;The input HTML string with comments removed&lt;/returns&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> WithoutComments<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> html<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;">&#40;</span>html, CommentPattern, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span>, ExpressionOptions<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Add a row to the input DataTable for each row match in the input MatchCollection</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;rowMatches&quot;&gt;A collection of all the rows to add to the DataTable&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;dataTable&quot;&gt;The DataTable to which we add rows&lt;/param&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> ParseRows<span style="color: #008000;">&#40;</span>MatchCollection rowMatches, DataTable dataTable<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Match rowMatch <span style="color: #0600FF; font-weight: bold;">in</span> rowMatches<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// if the row contains header tags don't use it - it is a header not a row</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>rowMatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;th&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                DataRow dataRow <span style="color: #008000;">=</span> dataTable<span style="color: #008000;">.</span><span style="color: #0000FF;">NewRow</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                MatchCollection cellMatches <span style="color: #008000;">=</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Matches</span><span style="color: #008000;">&#40;</span>
                    rowMatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span>,
                    CellPattern,
                    ExpressionOptions<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> columnIndex <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> columnIndex <span style="color: #008000;">&lt;</span> cellMatches<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> columnIndex<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    dataRow<span style="color: #008000;">&#91;</span>columnIndex<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> cellMatches<span style="color: #008000;">&#91;</span>columnIndex<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Groups</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
&nbsp;
                dataTable<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>dataRow<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Given a string containing an HTML table, parse the header cells to create a set of DataColumns</span>
    <span style="color: #008080; font-style: italic;">/// which define the columns in a DataTable.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;tableHtml&quot;&gt;An HTML string containing a single HTML table&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;A set of DataColumns based on the HTML table header cells&lt;/returns&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> DataColumn<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> ParseColumns<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> tableHtml<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        MatchCollection headerMatches <span style="color: #008000;">=</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Matches</span><span style="color: #008000;">&#40;</span>
            tableHtml,
            HeaderPattern,
            ExpressionOptions<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span> Match headerMatch <span style="color: #0600FF; font-weight: bold;">in</span> headerMatches
                <span style="color: #0600FF; font-weight: bold;">select</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DataColumn<span style="color: #008000;">&#40;</span>headerMatch<span style="color: #008000;">.</span><span style="color: #0000FF;">Groups</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// For tables which do not specify header cells we must generate DataColumns based on the number</span>
    <span style="color: #008080; font-style: italic;">/// of cells in a row (we assume all rows have the same number of cells).</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;rowMatches&quot;&gt;A collection of all the rows in the HTML table we wish to generate columns for&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;A set of DataColumns based on the number of celss in the first row of the input HTML table&lt;/returns&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> DataColumn<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> GenerateColumns<span style="color: #008000;">&#40;</span>MatchCollection rowMatches<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">int</span> columnCount <span style="color: #008000;">=</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Matches</span><span style="color: #008000;">&#40;</span>
            rowMatches<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
            CellPattern,
            ExpressionOptions<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span> index <span style="color: #0600FF; font-weight: bold;">in</span> Enumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">Range</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, columnCount<span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">select</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DataColumn<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Column &quot;</span> <span style="color: #008000;">+</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span>index<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>As always, here are the tests. They yield 100% coverage but I still need to add some asserts on the column names.</p>

<div class="wp_codebox"><table><tr id="p8778"><td class="code" id="p877code8"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Tests for the HtmlTableParser class</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008000;">&#91;</span>TestClass<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ParserTest
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> TestContext testContextInstance<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Verify that HtmlTableParser can parse an HTML file containing a single table. The</span>
    <span style="color: #008080; font-style: italic;">/// test file includes a commented out table which should be ignored. Note some tags use</span>
    <span style="color: #008080; font-style: italic;">/// attributes (we test we can parse tags with and without attributes).</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #008000;">&#91;</span>DeploymentItem<span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;data\singleTable.txt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestParseSingleTable<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> html <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAllText</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;singleTable.txt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        DataTable table <span style="color: #008000;">=</span> HtmlTableParser<span style="color: #008000;">.</span><span style="color: #0000FF;">ParseTable</span><span style="color: #008000;">&#40;</span>html<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        AssertTable<span style="color: #008000;">&#40;</span>GetExpectedData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, table<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Verify that HtmlTableParser can parse an HTML file containing multiple tables. The</span>
    <span style="color: #008080; font-style: italic;">/// test file includes a commented out table which should be ignored. The test file</span>
    <span style="color: #008080; font-style: italic;">/// contains tables both with and without headers.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
    <span style="color: #008000;">&#91;</span>DeploymentItem<span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;data\multipleTables.txt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestParseMultipleTables<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> html <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAllText</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;multipleTables.txt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        DataSet dataSet <span style="color: #008000;">=</span> HtmlTableParser<span style="color: #008000;">.</span><span style="color: #0000FF;">ParseDataSet</span><span style="color: #008000;">&#40;</span>html<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span>, dataSet<span style="color: #008000;">.</span><span style="color: #0000FF;">Tables</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        var expected <span style="color: #008000;">=</span> GetExpectedData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>DataTable table <span style="color: #0600FF; font-weight: bold;">in</span> dataSet<span style="color: #008000;">.</span><span style="color: #0000FF;">Tables</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            AssertTable<span style="color: #008000;">&#40;</span>expected, table<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> GetExpectedData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #008000;">&#123;</span>
            <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">&quot;row 1, cell 1&quot;</span>, <span style="color: #666666;">&quot;row 1, cell 2&quot;</span> <span style="color: #008000;">&#125;</span>,
            <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">&quot;row 2, cell 1&quot;</span>, <span style="color: #666666;">&quot;row 2, cell 2&quot;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> AssertTable<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> expected, DataTable table<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span>expected<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, table<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span>, <span style="color: #666666;">&quot;Table did not contain the expected number of rows&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> expected<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> expected<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #6666cc; font-weight: bold;">string</span> actualElement <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>table<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Trim</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #6666cc; font-weight: bold;">string</span> expectedElement <span style="color: #008000;">=</span> expected<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
                Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>expectedElement, actualElement, <span style="color: #666666;">&quot;Table did not contain the expected element&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>These are the test files, which are just some basic HMTL.</p>

<div class="wp_codebox"><table><tr id="p8779"><td class="code" id="p877code9"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
      &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot; /&gt;
    &lt;meta name=&quot;robots&quot; content=&quot;all&quot; /&gt;
      &lt;title&gt;Title&lt;/title&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
      &lt;!-- This table is commented out, so it shouldn't be parsed.
      &lt;table border=&quot;1&quot;&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;th&gt;Commented Heading 1&lt;/th&gt;
                  &lt;th&gt;Commented Heading 2&lt;/th&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;Commented row 1, cell 1&lt;/td&gt;
                  &lt;td&gt;Commented row 1, cell 2&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;Commented row 2, cell 1&lt;/td&gt;
                  &lt;td&gt;Commented row 2, cell 2&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt;
      --&gt;
&nbsp;
      &lt;!-- The parser should ignore the border attributes --&gt;
      &lt;table border=&quot;1&quot;&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;th&gt;Heading 1&lt;/th&gt;
                  &lt;th&gt;Heading 2&lt;/th&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                  &lt;td border=&quot;1&quot;&gt;row 1, cell 1&lt;/td&gt;
                  &lt;td&gt;row 1, cell 2&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;row 2, cell 1&lt;/td&gt;
                  &lt;td&gt;row 2, cell 2&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt; 
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p87710"><td class="code" id="p877code10"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
      &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot; /&gt;
    &lt;meta name=&quot;robots&quot; content=&quot;all&quot; /&gt;
      &lt;title&gt;Title&lt;/title&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
      &lt;!-- The parser should ignore the border attributes --&gt;
      &lt;table border=&quot;1&quot;&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;row 1, cell 1&lt;/td&gt;
                  &lt;td&gt;row 1, cell 2&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;row 2, cell 1&lt;/td&gt;
                  &lt;td&gt;row 2, cell 2&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt; 
      &lt;!-- This table is commented out, so it shouldn't be parsed.
      &lt;table border=&quot;1&quot;&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;th&gt;Commented Heading 1&lt;/th&gt;
                  &lt;th&gt;Commented Heading 2&lt;/th&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;Commented row 1, cell 1&lt;/td&gt;
                  &lt;td&gt;Commented row 1, cell 2&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;Commented row 2, cell 1&lt;/td&gt;
                  &lt;td&gt;Commented row 2, cell 2&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt;
      --&gt;
      &lt;table border=&quot;1&quot;&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;th&gt;Heading 1&lt;/th&gt;
                  &lt;th&gt;Heading 2&lt;/th&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;row 1, cell 1&lt;/td&gt;
                  &lt;td&gt;row 1, cell 2&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;row 2, cell 1&lt;/td&gt;
                  &lt;td&gt;row 2, cell 2&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt; 
      &lt;table&gt;
            &lt;tr&gt;
                  &lt;td&gt;row 1, cell 1&lt;/td&gt;
                  &lt;td&gt;row 1, cell 2&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr  border=&quot;1&quot;&gt;
                  &lt;td&gt;row 2, cell 1&lt;/td&gt;
                  &lt;td border=&quot;1&quot;&gt;row 2, cell 2&lt;/td&gt;
            &lt;/tr&gt;
      &lt;/table&gt; 
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2010/05/parsing-html-tables-into-system-data-datatable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Filtering sqlmetal output using XSLT</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2010/03/filtering-sqlmetal-output-using-xslt/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2010/03/filtering-sqlmetal-output-using-xslt/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 21:50:41 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[build target]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sqlmetal]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xslt]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=799</guid>
		<description><![CDATA[I needed to generate LINQ to SQL O/RM classes for a subset of the objects in a large database. Unfortunately, sqlmetal doesn&#8217;t provide a mechanism to filter it&#8217;s output, so I made my own using XSLT, a batch file and a custom build target. This post explains how. Overview When you invoke a build, a [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to generate <a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx">LINQ to SQL</a> O/RM classes for a subset of the objects in a large database. Unfortunately, <a href="http://msdn.microsoft.com/en-us/library/bb386987.aspx">sqlmetal</a> doesn&#8217;t provide a mechanism to filter it&#8217;s output, so I made my own using XSLT, a batch file and a custom build target. This post explains how. </p>
<p>
<strong>Overview</strong></p>
<p>When you invoke a build, a custom &#8220;BeforeBuild&#8221; build target runs to do the code generation. This code generation is driven by a batch file, this is what it does:</p>
<ol>
<li> Run sqlmetal to output a dbml file for the entire database schema</li>
<li> Using an XML configuration file and an XSLT transform, generate a test .cs file containing a reference to each expected table. When included in our project, this gives us a compile time test that we generated the expected classes (we make this internal).</li>
<li> Run a second XSLT transform based on the configuration file and the output a dbml file from step 1. In this step we prune the xml according to the config file. The result is a dbml file containing only the tables we specify in our configuration.</li>
<li> Run sqlmetal again using the result of step 3 as input. This time sqlmetal outputs our CSharp code for our Linq to Sql classes. </li>
</ol>
<p>The XSLT transforms are run from the command line using MSXSL.exe, which is available <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=2FB55371-C94E-4373-B0E9-DB4816552E41&#038;displaylang=en">here</a>.</p>
<p>The generated CSharp files are part of my Visual Studio project. After the BeforeBuild target is run (which does the code generation), the generated code is compiled into an Assembly.</p>
<p>
<strong>Custom build target</strong></p>
<p>These are the pertinent parts of my project file. You can see that inside my project directory, I created a directory called Prebuild where the work happens. This is where the Configuration.xml, DbmlPruner.xslt, GenerateTestTypes.xslt and generate.bat files live.</p>

<div class="wp_codebox"><table><tr id="p79916"><td class="code" id="p799code16"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Project</span> <span style="color: #000066;">ToolsVersion</span>=<span style="color: #ff0000;">&quot;3.5&quot;</span> <span style="color: #000066;">DefaultTargets</span>=<span style="color: #ff0000;">&quot;Build&quot;</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/developer/msbuild/2003&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #808080; font-style: italic;">&lt;!-- most of the project file omitted for brevity --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Target</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;BeforeBuild&quot;</span> <span style="color: #000066;">DependsOnTargets</span>=<span style="color: #ff0000;">&quot;GenerateDbClasses;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Target</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;GenerateDbClasses&quot;</span> <span style="color: #000066;">Inputs</span>=<span style="color: #ff0000;">&quot;Prebuild\Configuration.xml;Prebuild\DbmlPruner.xslt;Prebuild\GenerateTestTypes.xslt&quot;</span> <span style="color: #000066;">Outputs</span>=<span style="color: #ff0000;">&quot;MasterDb.Generated.cs;TestMasterDbTypes.cs&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Exec</span> <span style="color: #000066;">Command</span>=<span style="color: #ff0000;">&quot;$(ProjectDir)Prebuild\generate.bat $(ProjectDir) $(TargetName) master.dbml prunedmaster.dbml TestMasterDbTypes.cs MasterDb Configuration.xml master&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>
<strong>Configuration.xml</strong></p>

<div class="wp_codebox"><table><tr id="p79917"><td class="code" id="p799code17"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- This configuration is used to specify which tables should generate linq to SQL classes, </span>
<span style="color: #808080; font-style: italic;">  and to generate a sanity check class which verifies all the expected types exist in the </span>
<span style="color: #808080; font-style: italic;">  generated code.</span>
<span style="color: #808080; font-style: italic;">  --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Configuration</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;Master Tables&quot;</span> <span style="color: #000066;">SourceXml</span>=<span style="color: #ff0000;">&quot;master.dbml&quot;</span> <span style="color: #000066;">Namespace</span>=<span style="color: #ff0000;">&quot;Master.Data.Linq&quot;</span> <span style="color: #000066;">TestClassName</span>=<span style="color: #ff0000;">&quot;TestMasterTypes&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Table</span> <span style="color: #000066;">SqlName</span>=<span style="color: #ff0000;">&quot;dbo.TestTable&quot;</span> <span style="color: #000066;">DataContextPropertyName</span>=<span style="color: #ff0000;">&quot;TestTable&quot;</span> <span style="color: #000066;">ClassName</span>=<span style="color: #ff0000;">&quot;TestTableRow&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>
<strong>GenerateTestTypes.xslt</strong></p>

<div class="wp_codebox"><table><tr id="p79918"><td class="code" id="p799code18"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- ===========================================================</span>
<span style="color: #808080; font-style: italic;">  Generate a C# class with members corresponding to all the linq</span>
<span style="color: #808080; font-style: italic;">  to SQL tables specified in the input file.</span>
<span style="color: #808080; font-style: italic;">================================================================ --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:output</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
//-----------------------------------------------------------------------
// <span style="color: #339933;">&lt;![CDATA[&lt;auto-generated&gt;]]&gt;</span>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost 
//     if the code is regenerated.
// <span style="color: #339933;">&lt;![CDATA[&lt;/auto-generated&gt;]]&gt;</span>
//-----------------------------------------------------------------------
&nbsp;
// Disable warning CS0169: The private field 'foo' is never used. This is 
// by design.
#pragma warning disable 0169
&nbsp;
namespace <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;/Configuration/@Namespace&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
{
    /// <span style="color: #339933;">&lt;![CDATA[&lt;summary&gt;]]&gt;</span>
    /// This class is provided as a compile time test for the linq to SQL
    /// classes specified in Prebuild/configuration.xml. It will fail to
    /// compile if one of the dependent classes is not generated (or not
    /// generated with the expected name). This is by design.
    /// <span style="color: #339933;">&lt;![CDATA[&lt;/summary&gt;]]&gt;</span>
    internal class <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;/Configuration/@TestClassName&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    {      
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:for-each</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;/Configuration/Table&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #ddbb00;">&amp;#9;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #ddbb00;">&amp;#9;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>private <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@ClassName&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;' '&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;concat(@ClassName, 'Member')&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #ddbb00;">&amp;#xa;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/xsl:for-each<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #ddbb00;">&amp;#9;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>}
}
&nbsp;
#pragma warning restore 0169
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>
<strong>DbmlPruner.xslt</strong></p>

<div class="wp_codebox"><table><tr id="p79919"><td class="code" id="p799code19"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- ===========================================================</span>
<span style="color: #808080; font-style: italic;">  Replicate a .dbml file based on the tables specified in a </span>
<span style="color: #808080; font-style: italic;">  configuration.</span>
<span style="color: #808080; font-style: italic;">================================================================ --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span></span>
<span style="color: #009900;">	 <span style="color: #000066;">xmlns:sql</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/linqtosql/dbml/2007&quot;</span></span>
<span style="color: #009900;">     <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:output</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;xml&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000066;">indent</span>=<span style="color: #ff0000;">&quot;yes&quot;</span></span>
<span style="color: #009900;">  	  omit-xml-declaration = <span style="color: #ff0000;">&quot;no&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;Configuration&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!-- load sql metal output as $database --&gt;</span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:variable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;database&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;document(@SourceXml)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:variable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;namespace&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;namespace-uri($database/sql:Database)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:comment<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> =====================================================================================
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@Name&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> generated from database <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$database/sql:Database/@Name&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> (<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@SourceXml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>)
========================================================================================== <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:comment<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!-- Output a tree which replicates @SourceXml but contains only the table nodes in the configuration --&gt;</span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Database&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;{$namespace}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Name&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$database/sql:Database/@Name&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:for-each</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;/Configuration/Table&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:variable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sqlName&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@SqlName&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
				<span style="color: #808080; font-style: italic;">&lt;!-- only output a table element when the source table exists --&gt;</span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:if</span> <span style="color: #000066;">test</span>=<span style="color: #ff0000;">&quot;$database/sql:Database/sql:Table[@Name=$sqlName]&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
					<span style="color: #808080; font-style: italic;">&lt;!-- Output the table substituting Member and Type for the ClassName in the configuration --&gt;</span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Table&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;{$namespace}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Name&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
							<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@SqlName&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Member&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
							<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@DataContextPropertyName&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Type&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;{$namespace}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
							<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Name&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
								<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@ClassName&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
							<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
							<span style="color: #808080; font-style: italic;">&lt;!-- Copy the children (Columns etc) --&gt;</span>
							<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:copy-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$database/sql:Database/sql:Table[@Name=$sqlName]/sql:Type/*&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:if<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:for-each<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>
<strong>generate.bat</strong></p>
<p>This is my entire generate.bat file, you can glean the input arguments from the custom build target above.</p>

<div class="wp_codebox"><table><tr id="p79920"><td class="code" id="p799code20"><pre class="bat" style="font-family:monospace;">::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Generate LINQ to SQL classes based on tables defined in an xml config
::
&nbsp;
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
&nbsp;
setlocal 
&nbsp;
set SQLMETAL=&lt;put your path here!&gt;\sdk\Win2008\Bin\sqlmetal.exe
set MSXSL=&lt;put your path here!&gt;\msxsl.exe
&nbsp;
set PRJDIR=%1%
set PREDIR=%PRJDIR%Prebuild\
&nbsp;
set NAMESPACE=%2
&nbsp;
set METALOUT=%PREDIR%%3
set METALIN=%PREDIR%%4
&nbsp;
set COMPILETESTCLASS=%PRJDIR%%5
set CONTEXTCLASS=%6
set CONTEXTCLASSFILE=%PRJDIR%%CONTEXTCLASS%.Generated.cs
&nbsp;
set CONFIGPATH=%PREDIR%%7
set DATABASE=%8
&nbsp;
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
&nbsp;
echo ================ Generation of LINQ to SQL classes started ================
echo Using %CONFIGPATH%
&nbsp;
echo 1. Building %METALOUT% for entire %DATABASE% database 
%SQLMETAL% /conn:&quot;server=localhost; database=%DATABASE%;Integrated Security=SSPI&quot; /dbml:%METALOUT%
if errorlevel 1 goto :Failed
&nbsp;
echo 2. Generating %COMPILETESTCLASS% to test linq to sql types were generated correctly at compile time
%MSXSL% %CONFIGPATH% %PREDIR%GenerateTestTypes.xslt -o %COMPILETESTCLASS%
if errorlevel 1 goto :Failed
&nbsp;
echo 3. Building %METALIN% from %CONFIGPATH%
%MSXSL% %CONFIGPATH% %PREDIR%DbmlPruner.xslt -o %METALIN%
if errorlevel 1 goto :Failed
&nbsp;
echo 4. Generating %CONTEXTCLASSFILE% using %METALIN%
%SQLMETAL% %METALIN% /code:%CONTEXTCLASSFILE% /language:csharp /context:%CONTEXTCLASS% /namespace:%NAMESPACE% /serialization:Unidirectional
if errorlevel 1 goto :Failed
&nbsp;
echo ======== Generation of LINQ to SQL classes completed successfully =========
endlocal
goto :EOF
&nbsp;
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Failed
&nbsp;
echo Configuration %CONFIG% FAILED
endlocal
exit /B 1</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2010/03/filtering-sqlmetal-output-using-xslt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SqueezeServer WinForms Client</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2010/02/squeezeserver-winforms-client/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2010/02/squeezeserver-winforms-client/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 00:05:22 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Hi-Fi]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[squeezebox]]></category>
		<category><![CDATA[squeezecenter]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=761</guid>
		<description><![CDATA[A couple of days ago I finally got fed up with the SqueezeCenter web interface. It&#8217;s not that it&#8217;s bad, it&#8217;s just that I open a lot of tabs in my browser, then can&#8217;t find the SqueezeCenter page when I need it. I thought it would be nice to be able to control SqueezeCenter from [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago I finally got fed up with the SqueezeCenter web interface. It&#8217;s not that it&#8217;s bad, it&#8217;s just that I open a lot of tabs in my browser, then can&#8217;t find the SqueezeCenter page when I need it.</p>
<div id="attachment_789" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.hypercomplex.co.uk/wp-content/uploads/2010/02/player.jpg"><img src="http://blog.hypercomplex.co.uk/wp-content/uploads/2010/02/player-300x182.jpg" alt="" title="WinForms" width="300" height="182" class="size-medium wp-image-789" /></a><p class="wp-caption-text">A simple winforms Squeeze client.</p></div>
<p>I thought it would be nice to be able to control SqueezeCenter from something sitting in the taskbar, so over the last couple of days I made a simple .NET client. I&#8217;ve got 90% of what I need running, so as a proof of concept it has served its purpose.</p>
<p>Most of my effort was spent on data access and marshalling threads into the UI. Once I finish under the hood I might make a WPF UI layer, which would make it much more presentable.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2010/02/squeezeserver-winforms-client/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Combinations of IEnumerable&lt;T&gt;</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/11/combinations-of-ienumerablet/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/11/combinations-of-ienumerablet/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 21:16:46 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[combination]]></category>
		<category><![CDATA[extension method]]></category>
		<category><![CDATA[ienumerable]]></category>
		<category><![CDATA[iterator block]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=631</guid>
		<description><![CDATA[Today I needed to generate all the combinations of elements in a collection. After some fruitless searching I was forced to engage my brain, and came up with the following iterator block. I&#8217;m making no extravagant claims about performance (skip, take and count could all be faster). /// &#60;summary&#62; /// Compute the set of combinations, [...]]]></description>
			<content:encoded><![CDATA[<p>Today I needed to generate all the combinations of elements in a collection. After some fruitless searching I was forced to engage my brain, and came up with the following iterator block. I&#8217;m making no extravagant claims about performance (skip, take and count could all be faster).</p>

<div class="wp_codebox"><table><tr id="p63123"><td class="code" id="p631code23"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Compute the set of combinations, where a combination is an un-ordered collection </span>
<span style="color: #008080; font-style: italic;">/// of elements taken from the candidate set. Each element must appear zero or once. </span>
<span style="color: #008080; font-style: italic;">/// For set [1,2,3], the combinations are [1], [1,2], [1,2,3], [2], [2,3] and [3].</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;T&quot;&gt;The type of objects whose combinations we evaluate&lt;/typeparam&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;set&quot;&gt;The set from which to combine elements&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;The set of combinations&lt;/returns&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> IEnumerable<span style="color: #008000;">&lt;</span>IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> Combinations<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> set<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">int</span> n <span style="color: #008000;">=</span> set<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> skip <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> skip <span style="color: #008000;">&lt;</span> n<span style="color: #008000;">;</span> skip<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> take <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> take <span style="color: #008000;">&lt;=</span> n <span style="color: #008000;">-</span> skip<span style="color: #008000;">;</span> take<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> set<span style="color: #008000;">.</span><span style="color: #0000FF;">Skip</span><span style="color: #008000;">&#40;</span>skip<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Take</span><span style="color: #008000;">&#40;</span>take<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Just to prove it works, here are my tests.</p>

<div class="wp_codebox"><table><tr id="p63124"><td class="code" id="p631code24"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CombinationsOfNone<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var candidates <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var combinations <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    AssertEqualSetsOfSets<span style="color: #008000;">&#40;</span>combinations, candidates<span style="color: #008000;">.</span><span style="color: #0000FF;">Combinations</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CombinationsOfOne<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var candidates <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    var combinations <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> 
    <span style="color: #008000;">&#123;</span> 
        <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    AssertEqualSetsOfSets<span style="color: #008000;">&#40;</span>combinations, candidates<span style="color: #008000;">.</span><span style="color: #0000FF;">Combinations</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CombinationsOfThree<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var candidates <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    var combinations <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> 
    <span style="color: #008000;">&#123;</span> 
        <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#125;</span>, 
        <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#125;</span>,
        <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span><span style="color: #008000;">&#125;</span>,
        <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#125;</span>,
        <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span><span style="color: #008000;">&#125;</span>,
        <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    AssertEqualSetsOfSets<span style="color: #008000;">&#40;</span>combinations, candidates<span style="color: #008000;">.</span><span style="color: #0000FF;">Combinations</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> AssertEqualSetsOfSets<span style="color: #008000;">&#40;</span>List<span style="color: #008000;">&lt;</span>List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;&gt;</span> expected, IEnumerable<span style="color: #008000;">&lt;</span>IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;&gt;</span> actual<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>expected<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span>, actual<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var expectedIter <span style="color: #008000;">=</span> expected<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnumerator</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var actualIter <span style="color: #008000;">=</span> actual<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnumerator</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>expectedIter<span style="color: #008000;">.</span><span style="color: #0000FF;">MoveNext</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> actualIter<span style="color: #008000;">.</span><span style="color: #0000FF;">MoveNext</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>OrderIndependentEqual<span style="color: #008000;">&#40;</span>expectedIter<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span>, actualIter<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OrderIndependentEqual<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> first, IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> second<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">!</span>first<span style="color: #008000;">.</span><span style="color: #0000FF;">Except</span><span style="color: #008000;">&#40;</span>second<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Concat</span><span style="color: #008000;">&#40;</span>second<span style="color: #008000;">.</span><span style="color: #0000FF;">Except</span><span style="color: #008000;">&#40;</span>first<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Any</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/11/combinations-of-ienumerablet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Should Dispose throw exceptions?</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/11/should-dispose-throw-exceptions/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/11/should-dispose-throw-exceptions/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 23:47:51 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[IDisposable]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=560</guid>
		<description><![CDATA[The Framework Design Guidelines suggest that you should avoid throwing an exception from Dispose. This page has a better quote: Avoid throwing an exception from within Dispose except under critical situations where the containing process has been corrupted (leaks, inconsistent shared state, etc.). I can think of two good reasons to avoid throwing (depending on [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://my.safaribooksonline.com/9780321545671">Framework Design Guidelines</a> suggest that you should <em>avoid</em> throwing an exception from Dispose. <a href="http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae">This</a> page has a better quote: </p>
<p><em>Avoid throwing an exception from within Dispose except under critical situations where the containing process has been corrupted (leaks, inconsistent shared state, etc.).</em></p>
<p>I can think of two good reasons to avoid throwing (depending on your use case they may or may not represent the desired behaviour):</p>
<ul>
<li> If there is an unhandled exception in the finaliser thread, under .NET 2.0 and above, the <a href="http://msdn.microsoft.com/en-us/library/ms228965.aspx">process terminates</a>. Yikes. As MSDN <a href="http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx">states</a>, IDisposable classes should call Dispose from the finaliser to be sure Dispose is always called. </li>
<li> Exception masking: If dispose is triggered by a using block after an exception is thrown inside the block, then Dispose throws, you will get the exception from dispose, not the original.</li>
</ul>
<p>Rogue Dispose methods pop up in the .NET framework (e.g. in WCF), and in other <a href="http://blog.cornetdesign.com/2009/06/exceptions-in-constructors-how-reflection-helped-workaround-an-oracle-bug/">libraries</a>. If you have to contend with some dangerous disposal, Marc Gravell has a safe <a href="http://marcgravell.blogspot.com/2008/11/dontdontuse-using.html">IDisposable wrapper</a> template/extension method pattern. This could easily be modified to do some logging rather than silently swallowing.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/11/should-dispose-throw-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sorting</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/09/sorting/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/09/sorting/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 21:18:34 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Application Development Foundation]]></category>
		<category><![CDATA[IComparer]]></category>
		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=476</guid>
		<description><![CDATA[In the .NET 1.1 era, sorting is based on the IComparer and IComparable interfaces. I first realised this scheme is a little limited in a job interview. I tried to explain how I might sort a contact list on different properties. In my mind I envisaged something similar to the STL sort algorithm using a [...]]]></description>
			<content:encoded><![CDATA[<p>In the .NET 1.1 era, sorting is based on the <a href="http://msdn.microsoft.com/en-us/library/system.collections.icomparer.aspx">IComparer</a> and <a href="http://msdn.microsoft.com/en-us/library/system.icomparable.aspx">IComparable</a> interfaces. I first realised this scheme is a little limited in a job interview. I tried to explain how I might sort a contact list on different properties. In my mind I envisaged something similar to the <a href="http://www.cplusplus.com/reference/algorithm/sort/">STL sort algorithm</a> using a function pointer or <a href="http://en.wikipedia.org/wiki/Function_object">functor</a>. Unfortunately, at the time I was unable to express this in C#.</p>
<p>Let&#8217;s say our Contact data structure is simply this:</p>

<div class="wp_codebox"><table><tr id="p47632"><td class="code" id="p476code32"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Contact <span style="color: #008000;">:</span> IComparable
<span style="color: #008000;">&#123;</span>
   <span style="color: #008080; font-style: italic;">// constructor omitted, just assume it assigns fields</span>
&nbsp;
   <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Forename <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Surname <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> PhoneNo <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> EmailAddress <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
   <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> CompareTo<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> obj<span style="color: #008000;">&#41;</span> 
   <span style="color: #008000;">&#123;</span>
        Contact otherContact <span style="color: #008000;">=</span> obj <span style="color: #0600FF; font-weight: bold;">as</span> Contact<span style="color: #008000;">;</span> 
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>otherContact <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Surname</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CompareTo</span><span style="color: #008000;">&#40;</span>otherContact<span style="color: #008000;">.</span><span style="color: #0000FF;">Surname</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
           <span style="color: #0600FF; font-weight: bold;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ArgumentException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Object is not a Contact&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In order to sort a collection of Contacts, we can simply call the Sort method as follows:</p>

<div class="wp_codebox"><table><tr id="p47633"><td class="code" id="p476code33"><pre class="csharp" style="font-family:monospace;">ArrayList list <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ArrayList<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// add some Contacts</span>
list<span style="color: #008000;">.</span><span style="color: #0000FF;">Sort</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>That&#8217;s all well and good, but we will always sort by Contact.Surname. The collection Sort method will automatically use the IComparable CompareTo method defined by each Contact element. If we want to sort by a different property, say PhoneNo, we must use an IComparer.</p>

<div class="wp_codebox"><table><tr id="p47634"><td class="code" id="p476code34"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ContactPhoneNoComparer <span style="color: #008000;">:</span> IComparer
<span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Compare<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> left, <span style="color: #6666cc; font-weight: bold;">object</span> right<span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
      Contact first <span style="color: #008000;">=</span> left <span style="color: #0600FF; font-weight: bold;">as</span> Contact<span style="color: #008000;">;</span>
      Contact second <span style="color: #008000;">=</span> right <span style="color: #0600FF; font-weight: bold;">as</span> Contact<span style="color: #008000;">;</span>
      <span style="color: #008080; font-style: italic;">// null checking omitted for brevity</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> first<span style="color: #008000;">.</span><span style="color: #0000FF;">PhoneNo</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CompareTo</span><span style="color: #008000;">&#40;</span>second<span style="color: #008000;">.</span><span style="color: #0000FF;">PhoneNo</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>To sort the Contact collection by PhoneNo we can now do this:</p>

<div class="wp_codebox"><table><tr id="p47635"><td class="code" id="p476code35"><pre class="csharp" style="font-family:monospace;">ArrayList contacts <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ArrayList<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// add some Contacts</span>
contacts<span style="color: #008000;">.</span><span style="color: #0000FF;">Sort</span><span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ContactPhoneNoComparer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>At least we can centralise our comparison logic in comparer classes, but this .NET 1.1 way of doing things is a bit ugly. Since we are forced to use object, the compiler can&#8217;t do any type checking to save us from our own stupidity. We end up having to write boiler plate code to check our casts. These comparer classes are also a little verbose, especially if we need a lot of them. </p>
<p>What follows is shameless plagarism: when I read chapter 1 of <a href="http://csharpindepth.com/">C# in Depth</a> I realised that it contained what would have been a good answer to my interview question. This is a diversion from the framework foundation, but a worthy one.</p>
<p><strong>IComparer&lt;T&gt;</strong></p>
<p>The casting, which is really the most significant limitation, can be removed by using IComparer&lt;T&gt;. Note that this is now much shorter and more readable.</p>

<div class="wp_codebox"><table><tr id="p47636"><td class="code" id="p476code36"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ContactPhoneNoComparer <span style="color: #008000;">:</span> IComparer<span style="color: #008000;">&lt;</span>Contact<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Compare<span style="color: #008000;">&#40;</span>Contact left, Contact right<span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> left<span style="color: #008000;">.</span><span style="color: #0000FF;">PhoneNo</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CompareTo</span><span style="color: #008000;">&#40;</span>right<span style="color: #008000;">.</span><span style="color: #0000FF;">PhoneNo</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Using a lambda expression</strong></p>
<p>Using a lambda expression we can eliminate the IComparer entirely.</p>

<div class="wp_codebox"><table><tr id="p47637"><td class="code" id="p476code37"><pre class="csharp" style="font-family:monospace;">List<span style="color: #008000;">&lt;</span>Contact<span style="color: #008000;">&gt;</span> contacts <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>Contact<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// add some Contacts</span>
contacts<span style="color: #008000;">.</span><span style="color: #0000FF;">Sort</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>l, r<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> l<span style="color: #008000;">.</span><span style="color: #0000FF;">PhoneNo</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CompareTo</span><span style="color: #008000;">&#40;</span>r<span style="color: #008000;">.</span><span style="color: #0000FF;">PhoneNo</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><strong>Using the OrderBy extension method</strong></p>
<p>Finally, using an extension method, we can do something even more readable. This time, the original order of the list is preserved, and we get a new contact list in the specified order.</p>

<div class="wp_codebox"><table><tr id="p47638"><td class="code" id="p476code38"><pre class="csharp" style="font-family:monospace;">List<span style="color: #008000;">&lt;</span>Contact<span style="color: #008000;">&gt;</span> contacts <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>Contact<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// add some Contacts</span>
List<span style="color: #008000;">&lt;</span>Contact<span style="color: #008000;">&gt;</span> sortedContacts <span style="color: #008000;">=</span> contacts<span style="color: #008000;">.</span><span style="color: #0000FF;">OrderBy</span><span style="color: #008000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c<span style="color: #008000;">.</span><span style="color: #0000FF;">PhoneNo</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/09/sorting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Iteration</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/09/iteration/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/09/iteration/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 21:17:14 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Application Development Foundation]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[ienumerable]]></category>
		<category><![CDATA[indexing]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=473</guid>
		<description><![CDATA[Iteration in .NET can be acomplished in two ways: indexing and using the IEnumerable interface. Indexing is an elementary programming concept covered elsewhere. IEnumerable &#038; IEnumerator The IEnumerable interface would perhaps have been better named IForwardIteratable, since it defines a scheme for forward only iteration. IEnumerable specifies a single method, GetEnumerator, which returns an IEnumerator, [...]]]></description>
			<content:encoded><![CDATA[<p>Iteration in .NET can be acomplished in two ways: <a href="http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx">indexing</a> and using the <a href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx">IEnumerable</a> interface. Indexing is an elementary programming concept covered <a href="http://msdn.microsoft.com/en-us/library/2549tw02.aspx">elsewhere</a>.</p>
<p><strong>IEnumerable &#038; IEnumerator</strong></p>
<p>The IEnumerable interface would perhaps have been better named IForwardIteratable, since it defines a scheme for forward only iteration. IEnumerable specifies a single method, GetEnumerator, which returns an IEnumerator, the actual forward iterator. IEnumerator specifies three members:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.current.aspx">Current</a> property: the collection element to which the iterator is pointing.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.movenext.aspx">MoveNext</a> method: moves the iterator to the next element in the collection. Returns false when the iterator has passed the final element.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.reset.aspx">Reset</a> method: set the iterator to the initial state, where calling MoveNext results in the iterator pointing at the first element.</li>
</ul>
<p>You may then traverse a collection as follows:</p>

<div class="wp_codebox"><table><tr id="p47341"><td class="code" id="p473code41"><pre class="csharp" style="font-family:monospace;">IEnumerator enumerator <span style="color: #008000;">=</span> collection<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnumerator</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>enumerator<span style="color: #008000;">.</span><span style="color: #0000FF;">MoveNext</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>enumerator<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><strong>foreach</strong></p>
<p>The code snippet above might seem a little cumbersome. To improve matters both VB and C# provide the foreach language level construct. Using foreach the example above becomes simply:</p>

<div class="wp_codebox"><table><tr id="p47342"><td class="code" id="p473code42"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> element <span style="color: #0600FF; font-weight: bold;">in</span> collection<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>element<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Notice that this scheme is based on object rather than the derived type in the collection. So, if you don&#8217;t use generic collections, there <em>might</em> be an implicit cast from IEnumerator.Current to the target type.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/09/iteration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Collections</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/09/collections/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/09/collections/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 20:32:17 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Application Development Foundation]]></category>
		<category><![CDATA[boxing]]></category>
		<category><![CDATA[Collections]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=451</guid>
		<description><![CDATA[The overarching design of System.Collections is obscured by the poorly conceived nomenclature (others concur). Concrete collections in .NET are based on these key interfaces: IEnumerable simply provides a method to return an IEnumerator. IEnumerator provides the mechanism for forward only iteration. ICollection extends IEnumerable adding some basic machinery for thread safe access and a Count [...]]]></description>
			<content:encoded><![CDATA[<p>The overarching design of <a href="http://msdn.microsoft.com/en-us/library/system.collections.aspx">System.Collections</a> is obscured by the poorly conceived nomenclature (<a href="http://aspnetresources.com/blog/dotnet_collection_madness.aspx">others</a> concur). Concrete collections in .NET are based on these key interfaces:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx">IEnumerable</a> simply provides a method to return an <a href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx">IEnumerator</a>. IEnumerator provides the mechanism for forward only iteration.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.collections.icollection.aspx">ICollection</a> extends IEnumerable adding some basic machinery for thread safe access and a Count property. Note that ICollection does not provide any way of adding/removing elements.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx">IList</a> extends ICollection to support indexing and the facility to add and remove elements. ILists can be fixed size and/or read only.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.collections.idictionary.aspx">IDictionary</a> extends ICollection to support  key value pairs. Values may be added, retrieved and removed by key. Both the keys and the values can be retreived as ICollections. IDictionarys can be fixed size and/or read only.</li>
</ul>
<p><strong><a href="http://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx">ArrayList</a> : <a href="http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx">IList</a></strong></p>
<p>ArrayList provides an IList interface over an array which is dynamically resized as you insert elements. Iterating over an ArrayList returns items in the order they were inserted (unless it has been sorted).</p>
<p>The following snippet demonstrates adding, inserting and removing elements and ranges of elements.</p>

<div class="wp_codebox"><table><tr id="p45144"><td class="code" id="p451code44"><pre class="csharp" style="font-family:monospace;">ArrayList al <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ArrayList<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
al<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;this&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Add range requires an ICollection arg</span>
<span style="color: #6666cc; font-weight: bold;">string</span> <span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> strs <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">&quot;is&quot;</span>, <span style="color: #666666;">&quot;a&quot;</span>, <span style="color: #666666;">&quot;list&quot;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
al<span style="color: #008000;">.</span><span style="color: #0000FF;">AddRange</span><span style="color: #008000;">&#40;</span>strs<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
al<span style="color: #008000;">.</span><span style="color: #0000FF;">Insert</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #666666;">&quot;hello&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> more <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">&quot;non&quot;</span>, <span style="color: #666666;">&quot;sense&quot;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
al<span style="color: #008000;">.</span><span style="color: #0000FF;">InsertRange</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span>, more<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
al<span style="color: #008000;">.</span><span style="color: #0000FF;">RemoveAt</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
al<span style="color: #008000;">.</span><span style="color: #0000FF;">RemoveRange</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Note that runtime performance is similar to what you might expect if manipulating a raw array. For example, in the worst case, InsertRange is an O(<em>m</em> + <em>n</em>) operation, where <em>n</em> is the number of elements to be added and <em>m</em> is the number of elements in the ArrayList.</p>
<p><strong>Casting and boxing</strong></p>
<p>System.Collections classes come from a time before .NET generics. As a consequence, collection elements are <a href="http://msdn.microsoft.com/en-us/library/system.object.aspx">objects</a> &#8211; everything is cast to/from object as it is stored/retrieved. Value types also get boxed/uboxed. This is all additional overhead.</p>
<p>Contary to what it says in the Framework Foundation book, generics are *sometimes* faster because they avoid this casting and boxing (depending on your use case). Rico Mariani has an interesting <a href="http://blogs.msdn.com/ricom/archive/2005/08/25/performance-quiz-7-generics-improvements-and-costs.aspx">performance comparison</a> (the real killer is the repeated allocation of the ArrayList Enumerator).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/09/collections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Encoding and Decoding</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/09/encoding-and-decoding/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/09/encoding-and-decoding/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 17:52:34 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Application Development Foundation]]></category>
		<category><![CDATA[Decoding]]></category>
		<category><![CDATA[Encoding]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=294</guid>
		<description><![CDATA[Encoding is the process of transforming a sequence of characters into a sequence of bytes, decoding is the reversal of this process. It is important to employ the correct encoding format, and particular attention should be paid when performing low level string operations or working with programs designed for regions which use non latin alphabets. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx">Encoding</a> is the process of transforming a sequence of characters into a sequence of bytes, decoding is the reversal of this process. It is important to employ the correct encoding format, and particular attention should be paid when performing low level string operations or working with programs designed for regions which use non <a href="http://en.wikipedia.org/wiki/Basic_modern_Latin_alphabet">latin alphabets</a>.</p>
<p><strong>ASCII, ISO and Unicode</strong></p>
<p>The venerable <a href="http://en.wikipedia.org/wiki/ASCII">ASCII</a> encoding standard is the basis for modern character encoding. ASCII encodes non printable control characters, as well as a modern latin alphabet, digits, punctuation marks, and a few miscellaneous symbols. These values are encoded in 7-bits (0-127). ASCII does not standardise use of values 128 to 255 in 8-bits; different regions invented their own standards and this makes it difficult to exchange text encoded using potentially different standards across regions.</p>
<p>ANSI went some way toward solving this by defining standardised code pages consisting of both the standard ASCII values (0-127) and language specific values (128-255). For example, <a href="http://en.wikipedia.org/wiki/ISO/IEC_8859-1">ISO 8859-1</a> is intended for western european languages. Clearly, ISO 8859 encodings are still not truly interoperable, but at least you know which characters are represented between 128-255.</p>
<p><a href="http://en.wikipedia.org/wiki/Unicode">Unicode</a> was introduced almost twenty years ago to provide a world text encoding scheme, capable of encoding the characters used in every living language. Although initially restricted to 16-bits, there are now multiple Unicode encoding schemes.</p>
<p><strong>System.Text Encodings</strong></p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/system.text.utf32encoding.aspx">UTF32Encoding</a> is a UTF-32 encoding representing each code point as a 32-bit integer.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.text.unicodeencoding.aspx">UnicodeEncoding</a> is a UTF-16 encoding representing each code point as a sequence of one to two 16-bit integers.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.text.utf8encoding.aspx">UTF8Encoding</a> is a UTF-8 encoding representing each code point as a sequence of one to four bytes.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.text.utf7encoding.aspx">UTF7Encoding</a> is a UTF-7 encoding representing Unicode characters as sequences of 7-bit ASCII characters.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.text.asciiencoding.aspx">ASCIIEncoding</a> corresponds to the Windows code page 20127. ASCII characters are limited to the lowest 128 Unicode characters, from U+0000 to U+007F.</li>
<li>ANSI/ISO encodings can be accessed via the <a href="http://msdn.microsoft.com/en-us/library/system.text.encoding.getencoding.aspx">Encoding.GetEncoding</a> method. <a href="http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx">This</a> page gives a list of the supported encodings.</li>
</ul>
<p>I was struck by the inconsistent naming of these classes, and that they don&#8217;t follow Microsoft&#8217;s naming conventions for <a href="http://msdn.microsoft.com/en-us/library/ms229043.aspx">capitalisation</a>.</p>
<p><strong>A simple example</strong></p>
<p>Using <a href="http://msdn.microsoft.com/en-us/library/system.text.encoding.getencoding.aspx">Encoding.GetEncoding</a> we can retrieve a Western European codepage, then get some encoded bytes using the GetBytes method. This is only slightly more typing than using one of the standard encoding classes described above.</p>

<div class="wp_codebox"><table><tr id="p29448"><td class="code" id="p294code48"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> westernEuroBytes <span style="color: #008000;">=</span> Encoding<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEncoding</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Windows-1252&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetBytes</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;foo bar&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> utf16Bytes <span style="color: #008000;">=</span> Encoding<span style="color: #008000;">.</span><span style="color: #0000FF;">Unicode</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetBytes</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;foo bar&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><strong>Enumerating supported code pages</strong></p>
<p>You can enumerate the available encodings as follows, this essentially reproduces the table found <a href="http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx">here</a>.</p>

<div class="wp_codebox"><table><tr id="p29449"><td class="code" id="p294code49"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>EncodingInfo ei <span style="color: #0600FF; font-weight: bold;">in</span> Encoding<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEncodings</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{0}, {1}, {2}&quot;</span>, ei<span style="color: #008000;">.</span><span style="color: #0000FF;">CodePage</span>, ei<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>, ei<span style="color: #008000;">.</span><span style="color: #0000FF;">DisplayName</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Encoding &#038; File I/O</strong></p>
<p>If you use an encoding other than UTF7, you must explicitly declare it when reading from the file, as follows.</p>

<div class="wp_codebox"><table><tr id="p29450"><td class="code" id="p294code50"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>StreamWriter sw <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StreamWriter<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;filename.txt&quot;</span>, <span style="color: #0600FF; font-weight: bold;">false</span>, Encoding<span style="color: #008000;">.</span><span style="color: #0000FF;">Unicode</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    sw<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;foo bar&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>StreamReader sw <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StreamReader<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;filename.txt&quot;</span>, Encoding<span style="color: #008000;">.</span><span style="color: #0000FF;">Unicode</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> line <span style="color: #008000;">=</span> sw<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>If you encode the file using ASCII, you can decode it using ASCII, UTF-7 and UTF-8 encodings. UTF-16 and UTF-32 use larger bytes, and are therefore incompatible.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/09/encoding-and-decoding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
