<?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>Parallelcoding.com &#187; .NET</title>
	<atom:link href="http://www.parallelcoding.com/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.parallelcoding.com</link>
	<description></description>
	<lastBuildDate>Mon, 16 Aug 2010 11:55:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Toughest Developer Puzzle Ever</title>
		<link>http://www.parallelcoding.com/2009/07/02/toughest-developer-puzzle-ever/</link>
		<comments>http://www.parallelcoding.com/2009/07/02/toughest-developer-puzzle-ever/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 18:34:20 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[puzzle]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=463</guid>
		<description><![CDATA[Jeff Blankenburg has put together an incredible puzzle over at http://www.toughestdeveloperpuzzleever.com/tdpe/. Absolutely a ton of fun. It took me a few hours to get through it myself!]]></description>
			<content:encoded><![CDATA[<p>Jeff Blankenburg has put together an incredible puzzle over at <a href="http://www.toughestdeveloperpuzzleever.com/tdpe/">http://www.toughestdeveloperpuzzleever.com/tdpe/</a>. Absolutely a ton of fun. It took me a few hours to get through it myself!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/07/02/toughest-developer-puzzle-ever/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Left Joins in LINQ</title>
		<link>http://www.parallelcoding.com/2009/06/19/left-joins-in-linq/</link>
		<comments>http://www.parallelcoding.com/2009/06/19/left-joins-in-linq/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 14:15:10 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=438</guid>
		<description><![CDATA[For some reason it seems that I often need to perform a left join in LINQ. Every time I need to do this I find myself scouring the web one more time in order to remember how a left join works in LINQ. So how does it work? The best example that I&#8217;ve found is [...]]]></description>
			<content:encoded><![CDATA[<p>For some reason it seems that I often need to perform a left join in LINQ. Every time I need to do this I find myself scouring the web one more time in order to remember how a left join works in LINQ. So how does it work? The best example that I&#8217;ve found is <a href="http://geekswithblogs.net/AzamSharp/archive/2008/04/07/121103.aspx">here</a>. The example looks something like this:</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">var list <span style="color: #008000;">=</span> from r <span style="color: #0600FF;">in</span> dc.<span style="color: #0000FF;">tblRooms</span>
             join ui <span style="color: #0600FF;">in</span> dc.<span style="color: #0000FF;">tblUserInfos</span>
             on r.<span style="color: #0000FF;">UserName</span> equals ui.<span style="color: #0000FF;">UserName</span> into userrooms
             where r.<span style="color: #0000FF;">CourseID</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">1848</span>
             from ur <span style="color: #0600FF;">in</span> userrooms.<span style="color: #0000FF;">DefaultIfEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
             select <span style="color: #008000;">new</span><span style="color: #000000;">&#123;</span>
                 FirstName <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>ur.<span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;N/A&quot;</span> <span style="color: #008000;">:</span> ur.<span style="color: #0000FF;">FirstName</span>,
                 LastName <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>ur.<span style="color: #0000FF;">LastName</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;N/A&quot;</span> <span style="color: #008000;">:</span> ur.<span style="color: #0000FF;">LastName</span>,
                 RoomName <span style="color: #008000;">=</span> r.<span style="color: #0000FF;">Name</span>
              <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>I like this example a lot because it is very straight forward. Personally I would like to make the statement a bit more generic. In order to do that all we need to remember is that every Left Join has a left table A and a right table B. All the results from the A will be returned regardless of the join with B. In my example I will call table A the LEFT_TABLE and table B the RIGHT_TABLE.</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">var list <span style="color: #008000;">=</span>  from LT <span style="color: #0600FF;">in</span> LEFT_TABLE
	      join RT <span style="color: #0600FF;">in</span> RIGHT_TABLE
	      on LT.<span style="color: #0000FF;">key</span> equals RT.<span style="color: #0000FF;">KEY</span> into NEW_TABLE
              where <span style="color: #008000;">&lt;</span>CONDITIONS<span style="color: #008000;">&gt;</span>
              from NT <span style="color: #0600FF;">in</span> NEW_TABLE.<span style="color: #0000FF;">DefaultIfEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
              <span style="color: #008000;">&lt;</span>SELECT_STATEMENT<span style="color: #008000;">&gt;;</span></pre></td></tr></table></div>

<p>
The only problem that may occur with this left join occurs with the DefaultIfEmpty() operator. A better practice would be to pass in a default value so that we can know what to expect in return.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/06/19/left-joins-in-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Future of Web Pages</title>
		<link>http://www.parallelcoding.com/2009/06/11/the-future-of-web-pages/</link>
		<comments>http://www.parallelcoding.com/2009/06/11/the-future-of-web-pages/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 15:46:40 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=430</guid>
		<description><![CDATA[This morning I came across this jQuery library and it got me to thinking a little bit. All of the web sites that I currently have coded ( I don&#8217;t really do the design) tend to use .NET or PHP that is mashed up with some Javascript here and there. I&#8217;m wondering why we tend [...]]]></description>
			<content:encoded><![CDATA[<p>This morning I came across <a href="http://flowplayer.org/tools/performance.html">this</a> jQuery library and it got me to thinking a little bit. All of the web sites that I currently have coded ( I don&#8217;t really do the design) tend to use .NET or PHP that is mashed up with some Javascript here and there.</p>
<p><br/></p>
<p>I&#8217;m wondering why we tend to still use PHP and .NET except for things that are absolutely on the back end. Why would I use .NET or PHP to manipulate elements in a page when I can just use JavaScript?</p>
<p><br/></p>
<p>Maybe the web community should start developing pages that are JavaScript run and call PHP/.NET WebServices. That puts all the processing for display on the JS and all the data manipulation and retrieval where it should be &#8211; on the server.</p>
<p><br/></p>
<p>Any thoughts?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/06/11/the-future-of-web-pages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Capitalize A String in C#</title>
		<link>http://www.parallelcoding.com/2009/05/27/capitalize-a-string-in-c/</link>
		<comments>http://www.parallelcoding.com/2009/05/27/capitalize-a-string-in-c/#comments</comments>
		<pubDate>Wed, 27 May 2009 17:34:34 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=395</guid>
		<description><![CDATA[Capitalizing a string is a rather trivial task in C#. There are 2 ways to approach single word capitalization where each method includes 3 steps. Method 1 uses only strings and string methods while method 2 treats the letter to be capitalized as a char. Method 1: Get the first character as a string ( [...]]]></description>
			<content:encoded><![CDATA[<p>Capitalizing a string is a rather trivial task in C#. There are 2 ways to approach single word capitalization where each method includes 3 steps. Method 1 uses only strings and string methods while method 2 treats the letter to be capitalized as a char.</p>
<p><strong>Method 1:</strong></p>
<ol>
<li>Get the first character as a string ( stringToCapitalize.Substring(0,1) )</li>
<li>Transform the first character to uppercase ( stringToCapitalize.Substring(0,1).ToUpper() )</li>
<li>Append the rest of the string ( stringToCapitalize.Substring(0,1).ToUpper() + stringToCapitalize.Substring(1) )</li>
</ol>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> Capitalize<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> toCapitalize<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">try</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>toCapitalize.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            toCapitalize <span style="color: #008000;">=</span> toCapitalize.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToUpper</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> toCapitalize.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">catch</span><span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        ExceptionHandling.<span style="color: #0000FF;">ExceptionLogging</span><span style="color: #000000;">&#40;</span>ex, <span style="color: #666666;">&quot;Error:Capitalize.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">return</span> toCapitalize<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><br/><strong>Method 2:</strong></p>
<ol>
<li>Get the first character as a char ( stringToCapitalize[0] )</li>
<li>Transform the first character to uppercase using the char.toUpper method ( char.toUpper(stringToCapitalize[0]) )</li>
<li>Append the rest of the string ( char.toUpper(stringToCapitalize.Substring[0]) + stringToCapitalize.Substring(1) )</li>
</ol>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> Capitalize<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> toCapitalize<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">try</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>toCapitalize.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            toCapitalize <span style="color: #008000;">=</span> <span style="color: #FF0000;">char</span>.<span style="color: #0000FF;">ToUpper</span><span style="color: #000000;">&#40;</span>toCapitalize<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> toCapitalize.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">catch</span><span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        ExceptionHandling.<span style="color: #0000FF;">ExceptionLogging</span><span style="color: #000000;">&#40;</span>ex, <span style="color: #666666;">&quot;Error:Capitalize.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">return</span> toCapitalize<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Using either of these methods you could create an extension method as well. Something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> MyExtensions
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> Capitalize<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> <span style="color: #FF0000;">String</span> toCapitalize<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>toCapitalize.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            toCapitalize <span style="color: #008000;">=</span> toCapitalize.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToUpper</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> toCapitalize.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">return</span> toCapitalize<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/05/27/capitalize-a-string-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Replace HTML Text without Regular Expressions</title>
		<link>http://www.parallelcoding.com/2009/05/21/replace-html-text-without-regular-expressions/</link>
		<comments>http://www.parallelcoding.com/2009/05/21/replace-html-text-without-regular-expressions/#comments</comments>
		<pubDate>Thu, 21 May 2009 17:34:07 +0000</pubDate>
		<dc:creator>manatarms</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=339</guid>
		<description><![CDATA[One of my latest job requirements was replacing some basic American English words with some more British words ( color-colour, favor-favour, etc.). The original iteration of this project used JavaScript to scan the page content and the replace the words properly. The only problem with this version is that AJAX calls would make browsers complain [...]]]></description>
			<content:encoded><![CDATA[<p>One of my latest job requirements was replacing some basic American English words with some more British words ( color-colour, favor-favour, etc.). The original iteration of this project used JavaScript to scan the page content and the replace the words properly. The only problem with this version is that AJAX calls would make browsers complain about breaking the DOM. The script for this iteration was:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> replaceTerms<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>	
	<span style="color: #003366; font-weight: bold;">var</span> searchArray <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;favors&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;labors&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;colors&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;favor&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;labor&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;color&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> replaceArray <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;favours&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;labours&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;colours&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;favour&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;labour&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;colour&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>document.<span style="color: #660066;">body</span> <span style="color: #339933;">||</span> <span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">body</span>.<span style="color: #660066;">innerHTML</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">//alert(&quot;Sorry, for some reason the text of this page is unavailable. Searching will not work.&quot;);</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> bodyText <span style="color: #339933;">=</span> document.<span style="color: #660066;">body</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> searchArray.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		bodyText <span style="color: #339933;">=</span> doReplace<span style="color: #009900;">&#40;</span>bodyText<span style="color: #339933;">,</span> searchArray<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> replaceArray<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">//document.body.innerHTML = bodyText;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> doReplace<span style="color: #009900;">&#40;</span>bodyText<span style="color: #339933;">,</span> searchTerm<span style="color: #339933;">,</span> replaceWith<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// find all occurences of the search term in the given text, and add some &quot;highlight&quot; tags to them (we're not using a</span>
	<span style="color: #006600; font-style: italic;">// regular expression search, because we want to filter out matches that occur within HTML tags and script blocks, so</span>
	<span style="color: #006600; font-style: italic;">// we have to do a little extra validation)</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> newText <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> lcSearchTerm <span style="color: #339933;">=</span> searchTerm.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> lcBodyText <span style="color: #339933;">=</span> bodyText.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>bodyText.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//Get index of search term</span>
		i <span style="color: #339933;">=</span> lcBodyText.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>lcSearchTerm<span style="color: #339933;">,</span> i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//if we can't fine it, replace the newText with the BodyText and return</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			newText <span style="color: #339933;">+=</span> bodyText<span style="color: #339933;">;</span>
			bodyText <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// skip anything inside an HTML tag</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>bodyText.<span style="color: #660066;">lastIndexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&gt;&quot;</span><span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> bodyText.<span style="color: #660066;">lastIndexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;&quot;</span><span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// skip anything inside a &lt;script&gt; block</span>
				<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lcBodyText.<span style="color: #660066;">lastIndexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/script&gt;&quot;</span><span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> lcBodyText.<span style="color: #660066;">lastIndexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;script&quot;</span><span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
					<span style="color: #006600; font-style: italic;">//Get Ascii Representation</span>
					<span style="color: #003366; font-weight: bold;">var</span>	charCode <span style="color: #339933;">=</span> bodyText.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">charCodeAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #006600; font-style: italic;">//Is this uppercase</span>
					<span style="color: #003366; font-weight: bold;">var</span> isUpper <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>charCode <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">65</span> <span style="color: #339933;">&amp;&amp;</span> charCode <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">90</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #006600; font-style: italic;">//Do replacing</span>
					<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>isUpper<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
						newText <span style="color: #339933;">+=</span> bodyText.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span>  replaceWith.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> replaceWith.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
						newText <span style="color: #339933;">+=</span> bodyText.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span>  replaceWith <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
					bodyText <span style="color: #339933;">=</span> bodyText.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">+</span> searchTerm.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					lcBodyText <span style="color: #339933;">=</span> bodyText.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					i <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> newText<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Since we could not have our site breaking the DOM, the filtering was moved into the Visual Basic code behind of the project. The trick to filtering content in .NET is to leverage the Response.Filter property along with a custom class. This class will intercept the content and re-write it to however you see fit. More details on this can be found <a target="_blank" href="http://aspnetlibrary.com/articledetails.aspx?article=Use-Response.Filter-to-intercept-your-HTML">here</a>.</p>
<p>The first version of our filter used regular expressions in order to replace the appropriate words in the page. This also caused a severe problem: The RegEx was replacing properties of tags, css, and control names ( understand that the word &#8216;color&#8217; was being replaced ). My first attempt at a solution was to find the proper RegEx to replace words only inside paragraph tags. It turns out that writing a RegEx to parse HTML is nearly impossible <img src='http://www.parallelcoding.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>The solution? The original JavaScript code was migrated into the Visual Basic filter. This worked like a charm as it was using no RegExes and was also based upon the original code that worked. The final Visual Basic code is as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Private</span> <span style="color: #000080;">Function</span> doReplace(<span style="color: #000080;">ByVal</span> bodyText <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>, <span style="color: #000080;">ByVal</span> searchTerm <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>, <span style="color: #000080;">ByVal</span> replaceWith <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
	<span style="color: #000080;">Private</span> <span style="color: #000080;">Function</span> doReplace(<span style="color: #000080;">ByVal</span> bodyText <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>, <span style="color: #000080;">ByVal</span> searchTerm <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>, <span style="color: #000080;">ByVal</span> replaceWith <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
	<span style="color: #000080;">Dim</span> newText <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;&quot;</span>
	<span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = -1
	<span style="color: #000080;">Dim</span> lcSearchTerm <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = searchTerm.ToLower()
	<span style="color: #000080;">Dim</span> lcBodyText <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = bodyText.ToLower()
&nbsp;
	<span style="color: #000080;">While</span> bodyText.Length &gt; 0
		<span style="color: #008000;">'Get the index of the search term
</span>		i = lcBodyText.IndexOf(lcSearchTerm, i + 1)
&nbsp;
		<span style="color: #008000;">'If it isn't there, just return
</span>		<span style="color: #000080;">If</span> i &lt; 0 <span style="color: #000080;">Then</span>
			newText += bodyText
			bodyText = <span style="color: #800000;">&quot;&quot;</span>
		<span style="color: #000080;">Else</span>
			<span style="color: #008000;">'Avoid tags
</span>			<span style="color: #000080;">If</span> bodyText.LastIndexOf(<span style="color: #800000;">&quot;&gt;&quot;</span>, i) &gt;= bodyText.LastIndexOf(<span style="color: #800000;">&quot;&lt;&quot;</span>, i) <span style="color: #000080;">Then</span>
				<span style="color: #008000;">'Avoid scripts
</span>				<span style="color: #000080;">If</span> lcBodyText.LastIndexOf(<span style="color: #800000;">&quot;/script&gt;&quot;</span>, i) &gt;= lcBodyText.LastIndexOf(<span style="color: #800000;">&quot;&lt;script&quot;</span>, i) <span style="color: #000080;">Then</span>
					<span style="color: #008000;">'Is the first character uppercase?
</span>					<span style="color: #000080;">Dim</span> isUpper <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span> = Char.IsUpper(bodyText.Chars(i))
&nbsp;
					<span style="color: #008000;">'If it is, then capitalize the replacement
</span>					<span style="color: #000080;">If</span> isUpper <span style="color: #000080;">Then</span>
						newText += bodyText.Substring(0, i) + Char.ToUpper(replaceWith.Chars(0)) + replaceWith.Substring(1) + <span style="color: #800000;">&quot; &quot;</span>
					<span style="color: #000080;">Else</span>
						newText += bodyText.Substring(0, i) + replaceWith + <span style="color: #800000;">&quot; &quot;</span>
					<span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
&nbsp;
					<span style="color: #008000;">'Truncate body text
</span>					bodyText = bodyText.Substring(i + searchTerm.Length())
&nbsp;
					<span style="color: #008000;">'Reset current text
</span>					lcBodyText = bodyText.ToLower()
					i = -1
				<span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
			<span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
		<span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
	<span style="color: #000080;">End</span> <span style="color: #000080;">While</span>
&nbsp;
	Return newText
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span></pre></td></tr></table></div>

<p>The lesson learned here? Think before you code. If I would have simply migrated the JavaScript code I would have saved a lot of time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/05/21/replace-html-text-without-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
