<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>C# Code</title>
        <link>http://geekswithblogs.net/AnneBougie/category/8541.aspx</link>
        <description>Just a collection of some general coding to solve some common problems</description>
        <language>en-US</language>
        <copyright>Anne Bougie</copyright>
        <managingEditor>anne.bougie@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Sucky MSTest and the "WaitAll for multiple handles on a STA thread is not supported" Error</title>
            <link>http://geekswithblogs.net/AnneBougie/archive/2010/04/07/sucky-mstest-and-the-waitall-for-multiple-handles-on-a.aspx</link>
            <description>&lt;p&gt;If you are doing any multi-threading and are using MSTest, you will probably run across this error. For some reason, MSTest by default runs in STA threading mode. WTF, Microsoft! Why so stuck in the old COM world?  When I run the same test using NUnit, I don't have this problem. Unfortunately, my company has chosen MSTest, so I have a lot of testing problems. NUnit is so much better, IMO.&lt;/p&gt;
&lt;p&gt;After determining that I wasn't referencing any unmanaged code that would flip the thread into STA, which can also cause this error, the only thing left was the testing suite I was using. I dug around a little and found this obscure setting for the Test Run Config settings file that you can't set using its interface. You have to open it up as a text file and add the following setting. This is for VS2008. &lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red163\green21\blue21;\red255\green0\blue0;\red0\green0\blue0;}??\fs20 \cf1 &amp;lt;\cf3 ExecutionThread\cf1  \cf4 apartmentState\cf1 =\cf0 "\cf1 MTA\cf0 "\cf1  /&amp;gt;}
--&gt;&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: none repeat scroll 0% 0% white;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;ExecutionThread&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;apartmentState&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;MTA&lt;/span&gt;"&lt;span style="color: blue;"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;This didn't break any other tests, so I'm not sure why STA is the default, or why there is nothing in the test run configuration app to change this setting.&lt;/p&gt;
&lt;p&gt;Here is the code I was testing: &lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;}??\fs20       \cf3 public\cf0  \cf3 void\cf0  ProcessTest(\cf4 OldProcessInfo\cf0 [] infos)\par ??      \{\par ??         \cf4 WaitHandle\cf0 [] waits = \cf3 new\cf0  \cf4 WaitHandle\cf0 [infos.Length];\par ??         \cf3 int\cf0  i = 0;\par ??         \cf3 foreach\cf0  (\cf4 OldProcessInfo\cf0  info \cf3 in\cf0  infos)\par ??         \{\par ??            \cf4 AutoResetEvent\cf0  are = \cf3 new\cf0  \cf4 AutoResetEvent\cf0 (\cf3 false\cf0 );\par ??            info.Are = are;\par ??            waits[i++] = are;\par ??\par ??            \cf4 Processor\cf0  pr = \cf3 new\cf0  \cf4 Processor\cf0 ();\par ??            \cf4 WaitCallback\cf0  callback = pr.ProcessTest;\par ??            \cf4 ThreadPool\cf0 .QueueUserWorkItem(callback, info);\par ??         \}\par ??\par ??         \cf4 WaitHandle\cf0 .WaitAll(waits);\par ??      \}}
--&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: none repeat scroll 0% 0% white;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; ProcessTest(&lt;span style="color: rgb(43, 145, 175);"&gt;ProcessInfo&lt;/span&gt;[] infos)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: rgb(43, 145, 175);"&gt;WaitHandle&lt;/span&gt;[] waits = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;WaitHandle&lt;/span&gt;[infos.Length];&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: blue;"&gt;int&lt;/span&gt; i = 0;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;ProcessInfo&lt;/span&gt; info &lt;span style="color: blue;"&gt;in&lt;/span&gt; infos)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: rgb(43, 145, 175);"&gt;AutoResetEvent&lt;/span&gt; are = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;AutoResetEvent&lt;/span&gt;(&lt;span style="color: blue;"&gt;false&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      info.Are = are;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      waits[i++] = are;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: rgb(43, 145, 175);"&gt;Processor&lt;/span&gt; pr = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Processor&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: rgb(43, 145, 175);"&gt;WaitCallback&lt;/span&gt; callback = pr.Process;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: rgb(43, 145, 175);"&gt;ThreadPool&lt;/span&gt;.QueueUserWorkItem(callback, info);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: rgb(43, 145, 175);"&gt;WaitHandle&lt;/span&gt;.WaitAll(waits);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt; &lt;img src="http://geekswithblogs.net/AnneBougie/aggbug/139139.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Anne Bougie</dc:creator>
            <guid>http://geekswithblogs.net/AnneBougie/archive/2010/04/07/sucky-mstest-and-the-waitall-for-multiple-handles-on-a.aspx</guid>
            <pubDate>Wed, 07 Apr 2010 17:59:50 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/AnneBougie/comments/139139.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/AnneBougie/archive/2010/04/07/sucky-mstest-and-the-waitall-for-multiple-handles-on-a.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/AnneBougie/comments/commentRss/139139.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/AnneBougie/services/trackbacks/139139.aspx</trackback:ping>
        </item>
        <item>
            <title>Encrypt and Decrypt Strings</title>
            <link>http://geekswithblogs.net/AnneBougie/archive/2010/03/02/encrypt-and-decrypt-strings.aspx</link>
            <description>&lt;p&gt;Here is a quick solution to encrypting and decrypting passwords, or any strings, easily. Write a test that runs the GetNewKey method to generate a unique key. I put the key right in the class here for demonstration purposes, but you can store it anyplace. Write another test to encrypt your password so you know the value to save in your configuration settings, and you're all set. Just remember not to save your test with the clear text password in it!&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20    \cf3 public\cf0  \cf3 static\cf0  \cf3 class\cf0  \cf4 Helper\par ??\cf0    \{\par ??      \cf3 private\cf0  \cf3 static\cf0  \cf3 string\cf0  _key = \cf5 "DFQoC2KFQNdbs7fLUNjZQB9qDsbHLylC"\cf0 ;\par ??\par ??      \cf3 public\cf0  \cf3 static\cf0  \cf3 string\cf0  EncryptPassword(\cf3 this\cf0  \cf3 string\cf0  clearText)\par ??      \{\par ??         \cf3 byte\cf0 [] originalAsBytes = \cf4 Encoding\cf0 .ASCII.GetBytes(clearText);\par ??         \cf3 byte\cf0 [] result;\par ??         \cf3 byte\cf0 [] keyArray = \cf4 Convert\cf0 .FromBase64String(_key);\par ??         \cf3 using\cf0  (\cf3 var\cf0  tdes = \cf3 new\cf0  \cf4 TripleDESCryptoServiceProvider\cf0 ())\par ??         \{\par ??            tdes.Key = keyArray;\par ??            tdes.Mode = \cf4 CipherMode\cf0 .ECB;\par ??            tdes.Padding = \cf4 PaddingMode\cf0 .Zeros;\par ??\par ??            \cf4 ICryptoTransform\cf0  transform = tdes.CreateEncryptor();\par ??            result = transform.TransformFinalBlock(originalAsBytes, 0, originalAsBytes.Length);\par ??         \}\par ??\par ??         \cf3 string\cf0  encrypted = \cf4 Convert\cf0 .ToBase64String(result);\par ??         \cf3 return\cf0  encrypted;\par ??      \}\par ??\par ??      \cf3 public\cf0  \cf3 static\cf0  \cf3 string\cf0  DecryptPassword(\cf3 this\cf0  \cf3 string\cf0  encrypted)\par ??      \{\par ??         \cf3 byte\cf0 [] originalAsBytes = \cf4 Convert\cf0 .FromBase64String(encrypted);\par ??         \cf3 byte\cf0 [] keyArray = \cf4 Convert\cf0 .FromBase64String(_key);\par ??         \cf3 var\cf0  tdes = \cf3 new\cf0  \cf4 TripleDESCryptoServiceProvider\cf0  \{ Key = keyArray, Mode = \cf4 CipherMode\cf0 .ECB, Padding = \cf4 PaddingMode\cf0 .Zeros \};\par ??\par ??         \cf4 ICryptoTransform\cf0  transform = tdes.CreateDecryptor();\par ??         \cf3 byte\cf0 [] result = transform.TransformFinalBlock(originalAsBytes, 0, originalAsBytes.Length);\par ??         result = StripZeroPadding(result);\par ??         \cf3 string\cf0  clearText = \cf4 Encoding\cf0 .ASCII.GetString(result);\par ??         \cf3 return\cf0  clearText;\par ??      \}\par ??\par ??      \cf3 public\cf0  \cf3 static\cf0  \cf3 string\cf0  GetNewKey()\par ??      \{\par ??         \cf3 var\cf0  tdes = \cf3 new\cf0  \cf4 TripleDESCryptoServiceProvider\cf0  \{ KeySize = 192 \};\par ??         tdes.GenerateKey();\par ??         \cf3 byte\cf0 [] keyArray = tdes.Key;\par ??         \cf3 string\cf0  key = \cf4 Convert\cf0 .ToBase64String(keyArray);\par ??         \cf3 return\cf0  key;\par ??      \}\par ??\par ??      \cf3 private\cf0  \cf3 static\cf0  \cf3 byte\cf0 [] StripZeroPadding(\cf3 byte\cf0 [] array)\par ??      \{\par ??         \cf3 int\cf0  i = array.Length - 1;\par ??         \cf3 int\cf0  j = array.Length;\par ??         \cf3 while\cf0  (array[i] == 0)\par ??         \{\par ??            j = i;\par ??            -;\par ??         \}\par ??\par ??         \cf3 var\cf0  result = \cf3 new\cf0  \cf3 byte\cf0 [j];\par ??         \cf3 for\cf0  (i = 0; i &amp;lt; j; i++)\par ??            result[i] = array[i];\par ??         \cf3 return\cf0  result;\par ??      \}\par ??   \}}
--&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: none repeat scroll 0% 0% white;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Helper&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; _key = &lt;span style="color: rgb(163, 21, 21);"&gt;"DFQoC2KFQNdbs7fLUNjZQB9qDsbHLylC"&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; EncryptPassword(&lt;span style="color: blue;"&gt;this&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; clearText)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] originalAsBytes = &lt;span style="color: rgb(43, 145, 175);"&gt;Encoding&lt;/span&gt;.ASCII.GetBytes(clearText);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] result;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] keyArray = &lt;span style="color: rgb(43, 145, 175);"&gt;Convert&lt;/span&gt;.FromBase64String(_key);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: blue;"&gt;var&lt;/span&gt; tdes = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;TripleDESCryptoServiceProvider&lt;/span&gt;())&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;         tdes.Key = keyArray;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;         tdes.Mode = &lt;span style="color: rgb(43, 145, 175);"&gt;CipherMode&lt;/span&gt;.ECB;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;         tdes.Padding = &lt;span style="color: rgb(43, 145, 175);"&gt;PaddingMode&lt;/span&gt;.Zeros;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;         &lt;span style="color: rgb(43, 145, 175);"&gt;ICryptoTransform&lt;/span&gt; transform = tdes.CreateEncryptor();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;         result = transform.TransformFinalBlock(originalAsBytes, 0, originalAsBytes.Length);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;string&lt;/span&gt; encrypted = &lt;span style="color: rgb(43, 145, 175);"&gt;Convert&lt;/span&gt;.ToBase64String(result);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;return&lt;/span&gt; encrypted;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; DecryptPassword(&lt;span style="color: blue;"&gt;this&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; encrypted)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] originalAsBytes = &lt;span style="color: rgb(43, 145, 175);"&gt;Convert&lt;/span&gt;.FromBase64String(encrypted);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] keyArray = &lt;span style="color: rgb(43, 145, 175);"&gt;Convert&lt;/span&gt;.FromBase64String(_key);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;var&lt;/span&gt; tdes = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;TripleDESCryptoServiceProvider&lt;/span&gt; { Key = keyArray, Mode = &lt;span style="color: rgb(43, 145, 175);"&gt;CipherMode&lt;/span&gt;.ECB, Padding = &lt;span style="color: rgb(43, 145, 175);"&gt;PaddingMode&lt;/span&gt;.Zeros };&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: rgb(43, 145, 175);"&gt;ICryptoTransform&lt;/span&gt; transform = tdes.CreateDecryptor();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] result = transform.TransformFinalBlock(originalAsBytes, 0, originalAsBytes.Length);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      result = StripZeroPadding(result);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;string&lt;/span&gt; clearText = &lt;span style="color: rgb(43, 145, 175);"&gt;Encoding&lt;/span&gt;.ASCII.GetString(result);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;return&lt;/span&gt; clearText;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; GetNewKey()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;var&lt;/span&gt; tdes = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;TripleDESCryptoServiceProvider&lt;/span&gt; { KeySize = 192 };&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      tdes.GenerateKey();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] keyArray = tdes.Key;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;string&lt;/span&gt; key = &lt;span style="color: rgb(43, 145, 175);"&gt;Convert&lt;/span&gt;.ToBase64String(keyArray);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;return&lt;/span&gt; key;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] StripZeroPadding(&lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] array)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;int&lt;/span&gt; i = array.Length - 1;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;int&lt;/span&gt; j = array.Length;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;while&lt;/span&gt; (array[i] == 0)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;         j = i;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;         i--;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;var&lt;/span&gt; result = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[j];&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;for&lt;/span&gt; (i = 0; i &amp;lt; j; i++)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;         result[i] = array[i];&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;      &lt;span style="color: blue;"&gt;return&lt;/span&gt; result;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Here are the NUnit tests:&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red163\green21\blue21;}??\fs20       [\cf3 Test\cf0 ]\par ??      \cf4 public\cf0  \cf4 void\cf0  GetNewKeyTest()\par ??      \{\par ??         \cf4 string\cf0  key = \cf3 Helper\cf0 .GetNewKey();\par ??         \cf3 Console\cf0 .WriteLine(key);\par ??      \}\par ??\par ??      [\cf3 Test\cf0 ]\par ??      \cf4 public\cf0  \cf4 void\cf0  EncryptDecryptPasswordTest()\par ??      \{\par ??         \cf4 string\cf0  password = \cf5 "password"\cf0 ;\par ??         \cf4 string\cf0  encrypted = password.EncryptPassword();\par ??         \cf3 Console\cf0 .WriteLine(encrypted);\par ??         \cf3 Assert\cf0 .AreNotEqual(password, encrypted);\par ??\par ??         \cf4 string\cf0  decrypted = encrypted.DecryptPassword();\par ??         \cf3 Assert\cf0 .AreEqual(password, decrypted);\par ??      \}}
--&gt;
&lt;div style="font-family: Courier New; font-size: 10pt; color: black; background: none repeat scroll 0% 0% white;"&gt;
&lt;p style="margin: 0px;"&gt;[&lt;span style="color: rgb(43, 145, 175);"&gt;Test&lt;/span&gt;]&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; GetNewKeyTest()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: blue;"&gt;string&lt;/span&gt; key = &lt;span style="color: rgb(43, 145, 175);"&gt;Helper&lt;/span&gt;.GetNewKey();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(key);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;[&lt;span style="color: rgb(43, 145, 175);"&gt;Test&lt;/span&gt;]&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; EncryptDecryptPasswordTest()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: blue;"&gt;string&lt;/span&gt; password = &lt;span style="color: rgb(163, 21, 21);"&gt;"password"&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: blue;"&gt;string&lt;/span&gt; encrypted = password.EncryptPassword();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(encrypted);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.AreNotEqual(password, encrypted);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: blue;"&gt;string&lt;/span&gt; decrypted = encrypted.DecryptPassword();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;   &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.AreEqual(password, decrypted);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt; &lt;img src="http://geekswithblogs.net/AnneBougie/aggbug/138280.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Anne Bougie</dc:creator>
            <guid>http://geekswithblogs.net/AnneBougie/archive/2010/03/02/encrypt-and-decrypt-strings.aspx</guid>
            <pubDate>Tue, 02 Mar 2010 16:41:58 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/AnneBougie/comments/138280.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/AnneBougie/archive/2010/03/02/encrypt-and-decrypt-strings.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/AnneBougie/comments/commentRss/138280.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/AnneBougie/services/trackbacks/138280.aspx</trackback:ping>
        </item>
        <item>
            <title>Using Delegates in Rhino Mocks With the .Do Statement</title>
            <link>http://geekswithblogs.net/AnneBougie/archive/2009/08/21/using-delegates-in-rhino-mocks-with-the-.do-statement.aspx</link>
            <description>I use Rhino Mocks pretty much exclusively for mocking and testing. I find that there really isn't anything that I've wanted to do that I haven't been able to figure out how to accomplish with Rhino Mocks. What is everyone else using? I'd like to know so I can take a look.&lt;br /&gt;
&lt;br /&gt;
Anyway, as you know, with Expect.Call we can return property values, return values from functions, throw an error, etc. Sometimes just returning a static value isn't going to work, or it's just easier to give it some simple code to run. When you have many components, all with with their mockable interfaces, sometimes when you need to write a test that is in the middle, you need to run some simple code to test certain scenarios. I think this feature can be used inappropriately if it is overused, but it definitely has it's place when there's just no other good way to set up the test scenario.&lt;br /&gt;
&lt;br /&gt;
Here is a simple example of mocking out a billing component. I admit, it's a bit contrived, but demonstrates the use of delegates in Rhino Mocks. I have an interface named IBilling that I'm mocking. It's purpose is to bill several charges that fall within a billing period. The first four Expect.Call statements simply provide a date value. The .Do statement calls a delegate that passes in a function in my testing module. When your test code hits that function call, instead of just returning a predefined value, it will execute the function you passed in.&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;br /&gt;
[TestMethod]&lt;br /&gt;
private void ChargePeriodEndsBeforeBillingPeriodDailyChargeTest()&lt;br /&gt;
{&lt;br /&gt;
   MockRepository mocks = new MockRepository();&lt;br /&gt;
   IBilling billing = mocks.DynamicMock&amp;lt;IBilling&amp;gt;();&lt;br /&gt;
   IRateCalculator rates = mocks.DynamicMock&amp;lt;IRateCalculator&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
   int customerId = 1;&lt;br /&gt;
&lt;br /&gt;
   using (mocks.Record())&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;      Expect.Call(billing.GetPeriodBeginDate())&lt;br /&gt;
         .Return(DateTime.Parse("1/1/2010");&lt;br /&gt;
      Expect.Call(billing.GetChargeBe&lt;span style="font-family: Courier New;"&gt;ginDate&lt;/span&gt;())&lt;br /&gt;
         .Return(DateTime.Parse("1/1/2010");&lt;br /&gt;
      Expect.Call(billing.GetPeriodEndDate())&lt;br /&gt;
         .Return(DateTime.Parse("2/1/2010");&lt;br /&gt;
      Expect.Call(billing.GetChargeEndDate())&lt;br /&gt;
         .Return(DateTime.Parse("1/15/2010");&lt;br /&gt;
      Expect.Call(billing.CalculateChargeDays(&lt;br /&gt;
         new DateTime(), new DateTime()&lt;br /&gt;
         .IgnoreArguments()&lt;br /&gt;
         &lt;span style="color: rgb(0, 0, 255);"&gt;.Do(new CalcDaysDelegate(DaysDifference));&lt;/span&gt;&lt;br /&gt;
      Expect.Call(rates.GetRate(0)).Return(0.1);&lt;br /&gt;
      Expect.Call(&lt;span style="color: rgb(0, 0, 255);"&gt;() =&amp;gt; billing.AddCharge&lt;/span&gt;(0, 0, new DateTime()))&lt;br /&gt;
         .IgnoreArguments()&lt;br /&gt;
         .Constraints(Is.Equal(customerId), &lt;br /&gt;
                      Is.Equal(1.6), &lt;br /&gt;
                      Is.Anything());&lt;br /&gt;
   }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   using (mocks.Playback())&lt;br /&gt;
   {&lt;br /&gt;
      ChargeBilling chargeBilling = new ChargeBilling(&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;customerId, billing);&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;   &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;      chargeBilling.AddChargeToBill();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;   }&lt;br /&gt;
&lt;/span&gt;}&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;private delegate int CalcDaysDelegate(DateTime start, DateTime end);&lt;br /&gt;
private int DaysDifference(&lt;span style="font-family: Courier New;"&gt;DateTime start, DateTime end)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   TimeSpan ts = end - start;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   return ts.Days + 1;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Arial;"&gt;In real life I would recommend just returning a "16" to the CalculateChargeDays function call, but a real-life scenario would be too long of a post ; -) I also want to point out the use of the lambda expression for calling a function call with a void return value, &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Arial;"&gt;() =&amp;gt; billing.AddCharge&lt;/span&gt;. You can also call it using the delegate keyword, but I find the lambda syntax to be cleaner and easier to read, much less easier to remember when you're coding.&lt;br /&gt;
&lt;br /&gt;
Happy coding, and please let me know what you think about other mocking software you use that I might want to try.&lt;br /&gt;
&lt;br /&gt;&lt;div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a4f3c58c-ac72-4279-91a9-0c913d226319" style="margin: 0px; padding: 0px; display: inline; float: none;"&gt;Technorati Tags: &lt;br /&gt;
&lt;a href="http://technorati.com/tag/test-driven-development" rel="tag"&gt;Test Driven Development&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://technorati.com/tag/testing" rel="tag"&gt;Testing&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://technorati.com/tag/tfs" rel="tag"&gt;TFS&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://technorati.com/tags/Team+Foundation+Server" rel="tag"&gt;Team Foundation Server&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt; &lt;img src="http://geekswithblogs.net/AnneBougie/aggbug/134236.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Anne Bougie</dc:creator>
            <guid>http://geekswithblogs.net/AnneBougie/archive/2009/08/21/using-delegates-in-rhino-mocks-with-the-.do-statement.aspx</guid>
            <pubDate>Fri, 21 Aug 2009 15:51:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/AnneBougie/comments/134236.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/AnneBougie/archive/2009/08/21/using-delegates-in-rhino-mocks-with-the-.do-statement.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/AnneBougie/comments/commentRss/134236.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/AnneBougie/services/trackbacks/134236.aspx</trackback:ping>
        </item>
        <item>
            <title>Create an ADODB Recordset in code from XML</title>
            <link>http://geekswithblogs.net/AnneBougie/archive/2009/07/02/create-an-adodb-recordset-from-xml.aspx</link>
            <description>Recently I have been doing some work on an older system using ADODB recordsets. Every now and then we still have to support these old systems. I wanted to create an open recordset in code on the .NET side to pass to the older system, but ADODB really wants an open database connection in order to open a recordset. My first obvious attempt was to create the recordset object, then create the field objects and add them to the recordset. I could create the recordset this way, but I couldn't open it, and a recordset must be open in order to add data rows. What ADODB really wanted was a table that matched my schema so it could connect to it before adding rows. This was just not practicable. &lt;br /&gt;
&lt;br /&gt;
I discovered that you can open an ADODB recordset from an XML data source, and with the ADODB Stream object, that source can be in memory. Since this was something that I needed to use a lot for this project, I wrote a helper class, named RecordSetHelper, that will create the xml with an easy-to-use interface. I used Linq XML objects to create the XML string, then some rather simple code to put the pieces together into the correct schema that ADODB was expecting. If anyone wants the complete code, I can email it to them. The high points are below.&lt;br /&gt;
&lt;br /&gt;
Here is the xml for a simple ADODB recordset with a couple of data rows:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; background-color: rgb(255, 255, 255); color: rgb(0, 0, 128);"&gt;&amp;lt;xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" &lt;br /&gt;
     xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" &lt;br /&gt;
     xmlns:rs="urn:schemas-microsoft-com:rowset" &lt;br /&gt;
     xmlns:z="#RowsetSchema"&amp;gt;&lt;br /&gt;
  &amp;lt;s:Schema id="RowsetSchema"&amp;gt;&lt;br /&gt;
    &amp;lt;s:ElementType name="row" content="eltOnly" rs:updatable="true"&amp;gt;&lt;br /&gt;
      &amp;lt;s:AttributeType name="FirstName" rs:number="1" rs:write="true"&amp;gt;&lt;br /&gt;
        &amp;lt;s:datatype &lt;br /&gt;
            dt:type="string" &lt;br /&gt;
            rs:dbtype="str" &lt;br /&gt;
            dt:maxLength="200" &lt;br /&gt;
            rs:precision="0" &lt;br /&gt;
            rs:fixedlength="true" &lt;br /&gt;
            rs:maybenull="false" /&amp;gt;&lt;br /&gt;
      &amp;lt;/s:AttributeType&amp;gt;&lt;br /&gt;
      &amp;lt;s:AttributeType name="LastName" rs:number="2" rs:write="true"&amp;gt;&lt;br /&gt;
        &amp;lt;s:datatype &lt;br /&gt;
            dt:type="string" &lt;br /&gt;
            rs:dbtype="str" &lt;br /&gt;
            dt:maxLength="200" &lt;br /&gt;
            rs:precision="0" &lt;br /&gt;
            rs:fixedlength="true" &lt;br /&gt;
            rs:maybenull="false" /&amp;gt;&lt;br /&gt;
      &amp;lt;/s:AttributeType&amp;gt;&lt;br /&gt;
      &amp;lt;s:AttributeType name="DOB" rs:number="3" rs:write="true"&amp;gt;&lt;br /&gt;
        &amp;lt;s:datatype &lt;br /&gt;
            dt:type="dateTime" &lt;br /&gt;
            rs:dbtype="variantdate" &lt;br /&gt;
            dt:maxLength="16" &lt;br /&gt;
            rs:precision="0" &lt;br /&gt;
            rs:fixedlength="true" &lt;br /&gt;
            rs:maybenull="false" /&amp;gt;&lt;br /&gt;
      &amp;lt;/s:AttributeType&amp;gt;&lt;br /&gt;
      &amp;lt;s:extends type="rs:rowbase" /&amp;gt;&lt;br /&gt;
    &amp;lt;/s:ElementType&amp;gt;&lt;br /&gt;
  &amp;lt;/s:Schema&amp;gt;&lt;br /&gt;
  &amp;lt;rs:data&amp;gt;&lt;br /&gt;
    &amp;lt;rs:insert&amp;gt;&lt;br /&gt;
      &amp;lt;z:row &lt;br /&gt;
            FirstName="Hugh" &lt;br /&gt;
            LastName="Morris" &lt;br /&gt;
            DOB="1975-02-05T00:00:00" /&amp;gt;&lt;br /&gt;
      &amp;lt;z:row &lt;br /&gt;
            FirstName="Anne" &lt;br /&gt;
            LastName="Teak" &lt;br /&gt;
            DOB="1932-06-07T00:00:00" /&amp;gt;&lt;br /&gt;
      &amp;lt;z:row &lt;br /&gt;
            FirstName="Jean" &lt;br /&gt;
            LastName="Poole" &lt;br /&gt;
            DOB="1989-05-05T00:00:00" /&amp;gt;&lt;br /&gt;
    &amp;lt;/rs:insert&amp;gt;&lt;br /&gt;
  &amp;lt;/rs:data&amp;gt;&lt;br /&gt;
&amp;lt;/xml&amp;gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Creating the XML in code each time seemed rather tedious, so I made an interface to create a recordset using the RecordSetHelper. Here is an NUnit test that uses the RecordSetHelper to create an open recordset that is identical to the xml listed above.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      [Test]&lt;br /&gt;
      public void RecordsetHelperTest()&lt;br /&gt;
      {&lt;br /&gt;
         RecordSetHelper helper = new RecordSetHelper();&lt;br /&gt;
         helper.AddField(new FieldItem&lt;br /&gt;
            {&lt;br /&gt;
               Name = "FirstName", &lt;br /&gt;
               FieldType = VariantType.String&lt;br /&gt;
            });&lt;br /&gt;
         helper.AddField(new FieldItem&lt;br /&gt;
            {&lt;br /&gt;
               Name = "LastName", &lt;br /&gt;
               FieldType = VariantType.String&lt;br /&gt;
            });&lt;br /&gt;
         helper.AddField(new FieldItem&lt;br /&gt;
            {&lt;br /&gt;
               Name = "DOB", &lt;br /&gt;
               FieldType = VariantType.Date&lt;br /&gt;
            });&lt;br /&gt;
         helper.AddDataRow(new object[]&lt;br /&gt;
            {&lt;br /&gt;
               "Hugh", "Morris", "2/5/1975"&lt;br /&gt;
            });&lt;br /&gt;
         helper.AddDataRow(new object[]&lt;br /&gt;
            {&lt;br /&gt;
               "Anne", "Teak", "6/7/1932"&lt;br /&gt;
            });&lt;br /&gt;
         helper.AddDataRow(new object[]&lt;br /&gt;
            {&lt;br /&gt;
               "Jean", "Poole", "5/5/1989"&lt;br /&gt;
            });&lt;br /&gt;
         XDocument xRS = helper.MakeXML();&lt;br /&gt;
         ADODB.Recordset rs = helper.GetRecordSet();&lt;br /&gt;
         Assert.IsInstanceOf(typeof(ADODB.Recordset), rs);&lt;br /&gt;
         Assert.AreEqual((int)ObjectStateEnum.adStateOpen, rs.State);&lt;br /&gt;
      }&lt;br /&gt;
&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;br /&gt;
Here is the code to take the XML (returned by the MakeXml function) and turn it into an open ADODB recordset that you can manipulate. The stream is the ADODB.Stream object, not a .NET Stream object. &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      public Recordset GetRecordSet()&lt;br /&gt;
      {&lt;br /&gt;
         var doc = MakeXML();&lt;br /&gt;
         var rs = new Recordset();&lt;br /&gt;
         var stream = new Stream();&lt;br /&gt;
         stream.Open(&lt;br /&gt;
            Type.Missing, &lt;br /&gt;
            ConnectModeEnum.adModeUnknown, &lt;br /&gt;
            StreamOpenOptionsEnum.adOpenStreamUnspecified, &lt;br /&gt;
            null, &lt;br /&gt;
            null);&lt;br /&gt;
         stream.WriteText(doc.ToString(), StreamWriteEnum.adWriteChar);&lt;br /&gt;
         stream.Position = 0;&lt;br /&gt;
         rs.Open(&lt;br /&gt;
            stream, &lt;br /&gt;
            Type.Missing, &lt;br /&gt;
            CursorTypeEnum.adOpenUnspecified, &lt;br /&gt;
            LockTypeEnum.adLockUnspecified, 0);&lt;br /&gt;
&lt;br /&gt;
         return rs;&lt;br /&gt;
      }&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The MakeXML is the method that takes all the pieces needed to create the xml string and puts them all together.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      public XDocument MakeXML()&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         var root = MakeRoot();&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         var fields = GetFieldObjects();&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         var rowSetSchema = MakeFieldsXml(fields);&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         &lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         root.Add(rowSetSchema);&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         if (_rows.Count &amp;gt; 0)&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            var data = MakeDataXml();&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            root.Add(data);&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         var xdoc = new XDocument(root);&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         return xdoc;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      }&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The first thing to do was to create the XML namespace objects to be used when attaching the field and data elements. I created these as fields at the class level.&lt;br /&gt;
&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      internal XNamespace xns&lt;br /&gt;
      {&lt;br /&gt;
         get { return "http://www.w3.org/2000/xmlns/"; }&lt;br /&gt;
      }&lt;br /&gt;
      internal XNamespace xnss&lt;br /&gt;
      {&lt;br /&gt;
         get { return "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"; }&lt;br /&gt;
      }&lt;br /&gt;
      internal XNamespace xnsdt&lt;br /&gt;
      {&lt;br /&gt;
         get { return "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"; }&lt;br /&gt;
      }&lt;br /&gt;
      internal XNamespace xnsrs&lt;br /&gt;
      {&lt;br /&gt;
         get { return "urn:schemas-microsoft-com:rowset"; }&lt;br /&gt;
      }&lt;br /&gt;
      internal XNamespace xnsz&lt;br /&gt;
      {&lt;br /&gt;
         get { return "#RowsetSchema"; }&lt;br /&gt;
      }&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The "MakeRoot" function creates the root element, named "xml". The rest of the code just creates each piece and adds it to the root element. There is some funny kind of syntax needed to create the namespace attributes. You have to use the "+" operator with the XNamespace object and the prefix, then add the urn as the value. I struggled for a while with this and finally looked up the answer. It still seems like some strange idiomatic syntax, but it works. &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      private XElement MakeRoot()&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         var root = new XElement("xml",&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;             new XAttribute(xns + "s", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"),&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;             new XAttribute(xns + "dt", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"),&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;             new XAttribute(xns + "rs", "urn:schemas-microsoft-com:rowset"),&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;             new XAttribute(xns + "z", "#RowsetSchema"));&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         return root;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      }&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Since each field is the same schema, but differs in its name, datatype, and a few other attributes, I created an abstract base class named RSField. The public GetFieldElement method calls the abstract method GetDataTypeElement, which is what creates the distinct xml for each data type correctly. &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;   public abstract class RSField&lt;br /&gt;
   {&lt;br /&gt;
&lt;br /&gt;
      internal XNamespace xns&lt;br /&gt;
      {&lt;br /&gt;
         get { return "http://www.w3.org/2000/xmlns/"; }&lt;br /&gt;
      }&lt;br /&gt;
      internal XNamespace xnss&lt;br /&gt;
      {&lt;br /&gt;
         get { return "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"; }&lt;br /&gt;
      }&lt;br /&gt;
      internal XNamespace xnsdt&lt;br /&gt;
      {&lt;br /&gt;
         get { return "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"; }&lt;br /&gt;
      }&lt;br /&gt;
      internal XNamespace xnsrs&lt;br /&gt;
      {&lt;br /&gt;
         get { return "urn:schemas-microsoft-com:rowset"; }&lt;br /&gt;
      }&lt;br /&gt;
      protected XElement FieldElement;&lt;br /&gt;
      protected string FieldName { get; set; }&lt;br /&gt;
      protected int Ordinal { get; set; }&lt;br /&gt;
&lt;br /&gt;
      protected RSField(string fieldName, int ordinal)&lt;br /&gt;
      {&lt;br /&gt;
         FieldName = fieldName;&lt;br /&gt;
         Ordinal = ordinal;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      protected abstract XElement GetDataTypeElement();&lt;br /&gt;
&lt;br /&gt;
      public XElement GetFieldElement()&lt;br /&gt;
      {&lt;br /&gt;
         var element = new XElement(xnss + "AttributeType",&lt;br /&gt;
             new XAttribute("name", FieldName),&lt;br /&gt;
             new XAttribute(xnsrs + "number", Ordinal.ToString()),&lt;br /&gt;
             new XAttribute(xnsrs + "write", "true"));&lt;br /&gt;
         element.Add(GetDataTypeElement());&lt;br /&gt;
         return element;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Here is an example of an overriding class:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;   public class RSIntegerField : RSField&lt;br /&gt;
   {&lt;br /&gt;
      public RSIntegerField(string fieldName, int ordinal)&lt;br /&gt;
         : base(fieldName, ordinal)&lt;br /&gt;
      {&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      protected override XElement GetDataTypeElement()&lt;br /&gt;
      {&lt;br /&gt;
         var element = new XElement(xnss + "datatype",&lt;br /&gt;
             new XAttribute(xnsdt + "type", "i8"),&lt;br /&gt;
             new XAttribute(xnsdt + "maxLength", "8"),&lt;br /&gt;
             new XAttribute(xnsrs + "precision", "0"),&lt;br /&gt;
             new XAttribute(xnsrs + "fixedlength", "true"),&lt;br /&gt;
             new XAttribute(xnsrs + "maybenull", "false"));&lt;br /&gt;
         return element;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In the MakeXML method, after creating the root element, all the field objects are created. Each field object is created. The field objects are of type RSField, which contains the xml for each field.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      private RSField[] GetFieldObjects()&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         var fields = new RSField[_fields.Count];&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         for (int i = 0; i &amp;lt; _fields.Count; i++)&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            FieldItem field = _fields.ElementAt(i);&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            switch (field.FieldType)&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               case VariantType.Integer:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               case VariantType.Long:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  fields[i] = new RSIntegerField(field.Name, (i + 1));&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               case VariantType.Single:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  fields[i] = new RSSingleField(field.Name, (i + 1));&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               case VariantType.Double:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  fields[i] = new RSDoubleField(field.Name, (i + 1));&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               case VariantType.Currency:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  fields[i] = new RSCurrencyField(field.Name, (i + 1));&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               case VariantType.Date:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  fields[i] = new RSDateField(field.Name, (i + 1));&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               case VariantType.String:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  fields[i] = new RSTextField(field.Name, (i + 1));&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               case VariantType.Boolean:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  fields[i] = new RSBooleanField(field.Name, (i + 1));&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               case VariantType.Byte:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  fields[i] = new RSByteField(field.Name, (i + 1));&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               default:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;                  throw new ArgumentException("Field type is not valid");&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         return fields;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      }&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The objects collection that this is looping through are the type of a FieldItem class used to hold the name and data type of the field. In the NUnit test above, I added several fields using FieldItem.  I decided to use the Microsoft.VisualBasic namespace for the data type enum, but I could also have used the ADODB.DataTypeEnum, but the names seemed clunkier; "adBigInt" instead of just "Long". &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;   public class FieldItem&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;   {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      public string Name { get; set; }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      public VariantType FieldType { get; set; }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;   }&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The MakeFieldsXML, which is called from the MakeXML method, takes all the fields of RSField type and creates the Linq XElement tree for all the fields. This is then attached to the root element.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      private XElement MakeFieldsXml(IEnumerable&amp;lt;RSField&amp;gt; fields)&lt;br /&gt;
      {&lt;br /&gt;
         var schema = new XElement(xnss + "Schema",&lt;br /&gt;
               new XAttribute("id", "RowsetSchema"));&lt;br /&gt;
         var elementType = new XElement(xnss + "ElementType",&lt;br /&gt;
               new XAttribute("name", "row"),&lt;br /&gt;
               new XAttribute("content", "eltOnly"),&lt;br /&gt;
               new XAttribute(xnsrs + "updatable", "true"));&lt;br /&gt;
         foreach (var field in fields)&lt;br /&gt;
            elementType.Add(field.GetFieldElement());&lt;br /&gt;
         elementType.Add(new XElement(xnss + "extends",&lt;br /&gt;
            new XAttribute("type", "rs:rowbase")));&lt;br /&gt;
         schema.Add(elementType);&lt;br /&gt;
         return schema;&lt;br /&gt;
      }&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The data is passed to the RecordSetHelper in the form of an object array. The array must have the same number of elements as the number of fields. The MakeDataXML method creates the xml that contains all the data rows. &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      private XElement MakeDataXml()&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         var data = new XElement(xnsrs + "data");&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         var insert = new XElement(xnsrs + "insert");&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         foreach (var row in _rows)&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            var rowElement = new XElement(xnsz + "row");&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            for (int i = 0; i &amp;lt; _fields.Count; i++ )&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               var field = _fields.ElementAt(i);&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               object value = row[i];&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               string s = value.ToString(field.FieldType);&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               var val = new XAttribute(field.Name, s);&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               rowElement.Add(val);&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            insert.Add(rowElement);&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         data.Add(insert);&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         return data;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      }&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The only thing special about this piece of code is the ToString method on the value object-type variable. Just using the base ToString didn't work since it was of type "object", and the value may have been entered as a string, it may not have internally converted it to the correct type. Also, the date fields had to be in a certain format or it would crash when you retrieved the value from the recordset. So I created an extension method overriding the base ToString on object. It just converts the object to the correct underlying data type before converting to a string.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      public static string ToString(this object me, VariantType type)&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         string s;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         switch (type)&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            case VariantType.Integer:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               s = Convert.ToInt32(me).ToString();&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            case VariantType.Long:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               s = Convert.ToInt64(me).ToString();&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            case VariantType.Single:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               s = Convert.ToSingle(me).ToString();&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            case VariantType.Double:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               s = Convert.ToDouble(me).ToString();&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            case VariantType.Currency:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               s = Convert.ToDecimal(me).ToString();&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            case VariantType.Byte:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               s = Convert.ToByte(me).ToString();&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            case VariantType.Date:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               DateTime dtValue = DateTime.Parse(me.ToString());&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               s = dtValue.ToString("s");&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            case VariantType.Boolean:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               s = Convert.ToBoolean(me).ToString();&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            default:&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               s = Convert.ToString(me);&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;               break;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;            }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;         return s;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 128);"&gt;      }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 128);" /&gt;
&lt;br /&gt;
If you find this useful, and you would like the complete source code, let me know and I'll email you the project.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a4f3c58c-ac72-4279-91a9-0c913d226319" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;br /&gt;
&lt;a rel="tag" href="http://technorati.com/tags/adodb"&gt;ADODB&lt;/a&gt;&lt;br /&gt;
&lt;a rel="tag" href="http://technorati.com/tag/linq"&gt;Linq&lt;/a&gt;&lt;br /&gt;
&lt;a rel="tag" href="http://technorati.com/tag/.net"&gt;.NET&lt;/a&gt;&lt;br /&gt;
&lt;a rel="tag" href="http://technorati.com/r/tag/xml"&gt;XML&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt; &lt;img src="http://geekswithblogs.net/AnneBougie/aggbug/133227.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Anne Bougie</dc:creator>
            <guid>http://geekswithblogs.net/AnneBougie/archive/2009/07/02/create-an-adodb-recordset-from-xml.aspx</guid>
            <pubDate>Fri, 03 Jul 2009 00:54:40 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/AnneBougie/comments/133227.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/AnneBougie/archive/2009/07/02/create-an-adodb-recordset-from-xml.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/AnneBougie/comments/commentRss/133227.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/AnneBougie/services/trackbacks/133227.aspx</trackback:ping>
        </item>
        <item>
            <title>SharePoint Adventures - Reading an Excel Spreadsheet From A Stream</title>
            <link>http://geekswithblogs.net/AnneBougie/archive/2009/01/29/sharepoint-reading-an-excel-from-a-stream.aspx</link>
            <description>When I needed to read an Excel spreadsheet from a SharePoint site, it seemed like a simple enough request. Previously, whenever I needed to open an Excel file, I used an OleDb connection with the following connection string:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;       Data Source={0};Persist Security Info=False;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;       Extended Properties=""Excel 12.0;HDR=YES""";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;connectionString = string.Format(connectionString, filePath);&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Of course, you can't open the file from the SharePoint site this way. So, my first hurdle was to figure out how to download the file from the web site. The System.Net namespace provides a very easy way to do this:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;WebClient client = new WebClient();&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;client.UseDefaultCredentials = true;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;Stream stream = client.OpenRead(url);&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Now I had my file as a stream, so I thought I'd just extract it, save it to a temporary file, then read it in in the usual way. Problem solved. This next bit of code should probably be optimized a little so I'm not resizing the array every single loop:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;BinaryReader brdr = new BinaryReader(stream);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;byte[] result = new byte[0];&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;int bufferSize = 32768; // 32k&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;byte[] buffer = new byte[bufferSize];&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;long pos = 0;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;while (true)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;      buffer = brdr.ReadBytes(bufferSize);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;      if (pos &amp;gt; 0)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;      {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            // copy old data to bigger result&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            byte[] temp = new byte[result.LongLength];&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            Array.Copy(result, temp, result.LongLength);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            result = new byte[temp.LongLength + buffer.Length];&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            Array.Copy(temp, result, temp.LongLength);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            // add new data&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            for (int i = 0; i &amp;lt; buffer.Length; i++)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                result[pos + i] = buffer[i];&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            pos += buffer.Length;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;      }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;      else&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;      {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            result = new byte[buffer.Length];&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            Array.Copy(buffer, result, buffer.Length);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            pos = buffer.Length;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;      }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;      if (buffer.Length &amp;lt; bufferSize)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            break;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;string tempFile = Path.Combine(Environment.GetEnvironmentVariable("TMP"), &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;      "CopyList.xlsx");&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;using (var fs = new FileStream(tempFile, FileMode.OpenOrCreate))&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;      var writer = new BinaryWriter(fs);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;      writer.Write(result, 0, result.Length);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;      writer.Close();&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;      fs.Close();&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This worked beautifully on my development machine, but this is part of a larger program that is manipulating a SharePoint site via the SharePoint API. Therefore, it has to run on the server, which is 64-bit. When I first ran it, I received the error "The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine" . So, I did a little digging around, and found out that maybe I needed to install the driver. It is available from Microsoft as a &lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=7554F536-8C28-4598-9B72-EF94E038C891&amp;amp;displaylang=en"&gt;separate download&lt;/a&gt;, so I thought I was good to go. I installed it, and was still receiving the same error. Huh??!! Then I did a little *more* digging, and found out that there is no OleDb driver for opening Excel files on a 64-bit machine. Fine. No problem. Just compile the project as x86, and I'm good to go. Right? Well, wrong, actually. When the project is compiled in 32-bit, I can't access the SharePoint sites. Aaaaarrrrrrrhhhh! What to do? Then, I'm looking all over, trying to find a way to read an Excel file directly from a stream. How hard can it be, right?&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;&amp;lt;SARCASM&amp;gt;&lt;/span&gt;&lt;br /&gt;
Actually, Microsoft has this really &lt;a href="http://download.microsoft.com/download/0/B/E/0BE8BDD7-E5E8-422A-ABFD-4342ED7AD886/Excel97-2007BinaryFileFormat(xls)Specification.pdf"&gt;handy little guide&lt;/a&gt; that tells you just how to do it. It's only 349 pages. Turns out Excel's native format is something called a BIFF (Binary Interchange File Format). So, you just have to parse all the bytes from the stream into the right format. What a treat!&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;&amp;lt;/SARCASM&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
I know who I'd like to BIFF about now. Anyway, I did *even more* digging around, and I found some code from someone who has done just that. &lt;a href="http://www.codeplex.com/ExcelDataReader"&gt;Excel Data Reader&lt;/a&gt;  Thanks, iciobanu! However, it only works for Excel 2003 format. Now that is finally a problem I can easily solve! Save the spreadsheet to 2003 format, and read it in. Are we there yet? Almost! I was getting errors because the stream object returned from the web download didn't support seeking, which this code relies on. So, save the stream out to a temp file (back to that again), and read it back in as a FileStream object, which supports seeking, and my problems are, at long last, solved.&lt;br /&gt;
&lt;br /&gt;
And that is how you open an Excel file from a SharePoint site into a spreadsheet without using a generic OleDb connection. Sucess at long last! Isn't there some famous quote somewhere about persistence paying off? 
&lt;div style="margin: 0px; padding: 0px; display: inline; float: none;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a4f3c58c-ac72-4279-91a9-0c913d226319" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/SharePoint"&gt;SharePoint&lt;/a&gt;&lt;/div&gt; &lt;img src="http://geekswithblogs.net/AnneBougie/aggbug/129080.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>AnneBougie</dc:creator>
            <guid>http://geekswithblogs.net/AnneBougie/archive/2009/01/29/sharepoint-reading-an-excel-from-a-stream.aspx</guid>
            <pubDate>Thu, 29 Jan 2009 22:40:12 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/AnneBougie/comments/129080.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/AnneBougie/archive/2009/01/29/sharepoint-reading-an-excel-from-a-stream.aspx#feedback</comments>
            <slash:comments>5</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/AnneBougie/comments/commentRss/129080.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/AnneBougie/services/trackbacks/129080.aspx</trackback:ping>
        </item>
        <item>
            <title>InternalsVisibleTo with Strongly-Type Assemblies</title>
            <link>http://geekswithblogs.net/AnneBougie/archive/2008/10/01/internalsvisibleto-with-strongly-type-assemblies.aspx</link>
            <description>For testing, I often use the InternalsVisibleTo for my unit tests. It looks like this:&lt;br /&gt;
&lt;br style="font-weight: bold;" /&gt;
&lt;span style="font-weight: bold;"&gt;[assembly: InternalsVisibleTo("MyProject.Domain.Tests")]&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
When the assembly is strongly typed, though, this simple statement will not work, and we get the following message:
&lt;p style="font-weight: bold;"&gt;Error: "Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations"&lt;/p&gt;
To fix this, you must do two things: You must sign the test assembly, and you must include the public key in the InternalsVisibleTo statement. &lt;br /&gt;
&lt;br /&gt;
To get the public key value from the .snk file, use the sn.exe utility included in the .NET SDK. You must do two things, first, create a .snk file that contains only the public key:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;sn -p MyKey.snk MyKey.PublicKeyOnly.snk&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Then, you can extract the public key value with this command:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;sn -tp MyKey.PublicKeyOnly.snk&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This will produce a similar output:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Public key is&lt;/span&gt;&lt;br style="font-weight: bold;" /&gt;
&lt;span style="font-weight: bold;"&gt;0024000004800000940000000602000000240000525341310004000001000100cfb8bc23b86a08&lt;/span&gt;&lt;br style="font-weight: bold;" /&gt;
&lt;span style="font-weight: bold;"&gt;e70d021dd53d3b0293e716e71015870bdcc58a0231a4228618851a83e06077f5a44f42beb2baf3&lt;/span&gt;&lt;br style="font-weight: bold;" /&gt;
&lt;span style="font-weight: bold;"&gt;56ad2d344521a96b0081ed0f25f9227523e3625eda524efe1cf2e1e5e41f3693a76ec52347684b&lt;/span&gt;&lt;br style="font-weight: bold;" /&gt;
&lt;span style="font-weight: bold;"&gt;8129a4bb2d5fc49681adf33da0eecc4f81f011af4539d12abe1b4e760b5ce32d766db1012d4402&lt;/span&gt;&lt;br style="font-weight: bold;" /&gt;
&lt;span style="font-weight: bold;"&gt;8381f0b4&lt;/span&gt;&lt;br style="font-weight: bold;" /&gt;
&lt;br style="font-weight: bold;" /&gt;
&lt;span style="font-weight: bold;"&gt;Public key token is 2ff2b71993eeff95&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Copy the public key value and update the InternalsVisibleTo:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;[assembly: InternalsVisibleTo("MyProject.Domain.Tests, PublicKey=&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;
0024000004800000940000000602000000240000525341310004000001000100cfb8bc23b86a08&lt;/span&gt;&lt;br style="font-weight: bold;" /&gt;
&lt;span style="font-weight: bold;"&gt;e70d021dd53d3b0293e716e71015870bdcc58a0231a4228618851a83e06077f5a44f42beb2baf3&lt;/span&gt;&lt;br style="font-weight: bold;" /&gt;
&lt;span style="font-weight: bold;"&gt;56ad2d344521a96b0081ed0f25f9227523e3625eda524efe1cf2e1e5e41f3693a76ec52347684b&lt;/span&gt;&lt;br style="font-weight: bold;" /&gt;
&lt;span style="font-weight: bold;"&gt;8129a4bb2d5fc49681adf33da0eecc4f81f011af4539d12abe1b4e760b5ce32d766db1012d4402&lt;/span&gt;&lt;br style="font-weight: bold;" /&gt;
&lt;span style="font-weight: bold;"&gt;8381f0b4&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;")]&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Voila! You can now access the internals from your test project for strong named assemblies. &lt;img src="http://geekswithblogs.net/AnneBougie/aggbug/125571.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>AnneBougie</dc:creator>
            <guid>http://geekswithblogs.net/AnneBougie/archive/2008/10/01/internalsvisibleto-with-strongly-type-assemblies.aspx</guid>
            <pubDate>Wed, 01 Oct 2008 14:18:41 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/AnneBougie/comments/125571.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/AnneBougie/archive/2008/10/01/internalsvisibleto-with-strongly-type-assemblies.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/AnneBougie/comments/commentRss/125571.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/AnneBougie/services/trackbacks/125571.aspx</trackback:ping>
        </item>
        <item>
            <title>Matching and Grouping Regular Expressions using Regex in C#</title>
            <link>http://geekswithblogs.net/AnneBougie/archive/2008/09/17/matching-and-grouping-regular-expressions-using-regex-in-c-again.aspx</link>
            <description>Regular expressions are one of those things that you may not need very often, but when you do, it really solves the problem. The usage of the Regex static methods may seem difficult, but they're pretty easy. Being able to use regular expressions readily will help you quickly write some code that would take you many hours longer by parsing the strings.  I've compiled some code that uses the basic features of the Regex class. This article only covers the coding side of it. In order for it to work, you of course will need to know how to write the expressions themselves. I plan to cover that in my next post on Regex. That, too, seems very complex and difficult until you get a little experience with it. &lt;br /&gt;
&lt;br /&gt;
To simply determine if a string contains a match, use IsMatch:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;public bool IsValidUSPhone(string number)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    return Regex.IsMatch(number, @"\(\d{3}\)\s\d{3}-\d{4}");&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The Match object contains a lot of information about the matched sub-string. As you can see in the following code, it has a Success property that will return true if a match was found. The Value property contains the matched sub-string, as in the following code:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;string text = "Please call us at (808) 867-5309 to inquire about our offer.";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;string expression = @"\(\d{3}\)\s\d{3}-\d{4}";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;Match m = Regex.Match(text, expression);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;if (m.Success)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    Console.WriteLine(string.Format("Phone: {0}", m.Value));&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
If the text contains several sub-strings that will match the expression, the Match object will contain all of them, and you can loop through them as in the following code. Notice that I'm also using the Index and Length properties. &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;string text = "Please call us at (808) 867-5309 or (808) 555-1212 at your earliest convenience.";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;string expression = @"\(\d{3}\)\s\d{3}-\d{4}";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;Match m = Regex.Match(text, expression);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;while (m.Success)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    Console.WriteLine(string.Format("The phone # {0} starts at position {1}, and is {2} characters long.", m.Value, m.Index, m.Length));&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    m = m.NextMatch();&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
By using parentheses around sections of the match expression, you can pull out parts of the match value. The Match object contains a Groups property. When there is a match, it always has one item in the Groups collection, which is the matched value. When you use parentheses around parts of the expression, it puts those parts into Groups items. I added parentheses around the area code match, and around the phone number match.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;string text = "Please call us at (808) 867-5309 or (808) 555-1212 at your earliest convenience.";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;string expression = @"\((\d{3})\)\s(\d{3}-\d{4})";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;Match m = Regex.Match(text, expression);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;while (m.Success)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    Console.WriteLine("Match: {0}  Area code: {1}  Phone #: {2}", m.Groups[0], m.Groups[1], m.Groups[2]);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    m = m.NextMatch();&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This is good for simple tasks, but the MatchCollection is much better for more complex code.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;string text = "Please call us at (808) 867-5309 or (808) 555-1212 at your earliest convenience.";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;string expression = @"\(\d{3}\)\s\d{3}-\d{4}";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;MatchCollection matches = Regex.Matches(text, expression);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;Console.WriteLine("There are {0} phone numbers in the string.", matches.Count);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;foreach (Match m in matches)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    Console.WriteLine(string.Format("Phone: {0}", m.Value));&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
If you wanted to perform Linq queries against a match collection, you first have to get it into a list that implements IEnumerable. I'm sure there are many ways to do this, but here is one way:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;List&amp;lt;Match&amp;gt; mlist = matches.OfType&amp;lt;Match&amp;gt;().ToList();&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Replacing text is so much better using the Regex.Replace instead of the standard String replace method. Here is a very simple replace, which will simple replace (808) with (800). It will replace all occurrences of (808). Notice that in the replace expression I don't need to escape the parentheses characters:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;string text = "Please call us at (808) 867-5309 or (808) 555-1212 at your earliest convenience.";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;string matchExpression = @"\(808\)";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;string replaceExpression = @"(800)";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;string newText = Regex.Replace(text, matchExpression, replaceExpression);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;Console.WriteLine(newText);&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Here is an example of reformatting the phone number to 800.555.1212 format. I'm using parentheses to group sections of the match. In the replace expression, you can refer to groups by their index using the "$" character. In the match expression, the $ is to match the end of the string, but in the replace expression, it refers to the matched sub-group.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;string text = "Please call us at (808) 867-5309 or (808) 555-1212 at your earliest convenience.";&lt;br /&gt;
string matchExpression = @"\((\d{3})\)\s(\d{3})-(\d{4})";&lt;br /&gt;
string replaceExpression = @"$1.$2.$3";&lt;br /&gt;
string newText = Regex.Replace(text, matchExpression, replaceExpression);&lt;br /&gt;
Console.WriteLine(newText);&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
There are many things you cannot do, however, with the replace expression. An example is to change the case of the matched value. If you wanted to match html tags and make them all lowercase, for example, you would not be able to use the following match and replace expressions:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;string text = "&amp;lt;P&amp;gt;&amp;lt;H1&amp;gt;My Heading&amp;lt;h1&amp;gt;&amp;lt;p&amp;gt;";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;string matchExpression = @"((?:&amp;lt;/?)\w+)(?=.*?&amp;gt;)";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;string replaceExpression = "$1";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;string newText = Regex.Replace(text, matchExpression, replaceExpression.ToLower());&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;Console.WriteLine(newText);&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The ToLower() operates on the replace expression itself, not on the text it's replacing. In order to do this, and other complex replaces, you need to use a MatchEvaluator. This is really easy to do. Before I ever used it, I saw the word "delegate" and thought it would difficult to figure out, but it's so easy. You need to have a function that accepts a Match object as a parameter and returns a string to create a MatchEvaluator. Below is the code to replace the html tags with all lower case tags:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;public string HtmlTagsToLower(string html)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    string matchExpression = @"((?:&amp;lt;/?)\w+)(?=.*?&amp;gt;)";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    MatchEvaluator tolower = MatchEvaluatorToLower;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    return Regex.Replace(html, matchExpression, tolower);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;private string MatchEvaluatorToLower(Match m)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    return m.Value.ToLower();&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
I hope this helps to understand how to use the Regex methods. &lt;img src="http://geekswithblogs.net/AnneBougie/aggbug/125267.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>AnneBougie</dc:creator>
            <guid>http://geekswithblogs.net/AnneBougie/archive/2008/09/17/matching-and-grouping-regular-expressions-using-regex-in-c-again.aspx</guid>
            <pubDate>Thu, 18 Sep 2008 04:09:32 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/AnneBougie/comments/125267.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/AnneBougie/archive/2008/09/17/matching-and-grouping-regular-expressions-using-regex-in-c-again.aspx#feedback</comments>
            <slash:comments>5</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/AnneBougie/comments/commentRss/125267.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/AnneBougie/services/trackbacks/125267.aspx</trackback:ping>
        </item>
        <item>
            <title>Recursive Search for a Single File</title>
            <link>http://geekswithblogs.net/AnneBougie/archive/2008/08/16/recursive-search-for-a-single-file.aspx</link>
            <description>I recently had to write a routine to search for the location of a particular file in a directory tree. I could find lots of code to create an array of all the filenames in the tree, but I needed to just retrieve the location of the filename I was looking for. So, here's the code:&lt;br /&gt;
&lt;br /&gt;
&lt;font size="2"&gt;&lt;span style="font-family: Courier New;" /&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;/span&gt;&lt;br /&gt;
        public static string SearchForSingleFile(string path, string fileName)&lt;br /&gt;
        {&lt;br /&gt;
            string searchPath = Path.Combine(path, fileName);&lt;br /&gt;
&lt;br /&gt;
            if (File.Exists(searchPath))&lt;br /&gt;
            {&lt;br /&gt;
                return searchPath;&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            string foundFile = string.Empty;&lt;br /&gt;
            string[] dirs = Directory.GetDirectories(path);&lt;br /&gt;
&lt;br /&gt;
            foreach (string dir in dirs)&lt;br /&gt;
            {&lt;br /&gt;
                foundFile = SearchForSingleFile(dir, fileName);&lt;br /&gt;
&lt;br /&gt;
                if (foundFile != String.Empty)&lt;br /&gt;
                    break;&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            return foundFile;&lt;br /&gt;
        }&lt;/span&gt;&lt;/font&gt; &lt;img src="http://geekswithblogs.net/AnneBougie/aggbug/124480.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>AnneBougie</dc:creator>
            <guid>http://geekswithblogs.net/AnneBougie/archive/2008/08/16/recursive-search-for-a-single-file.aspx</guid>
            <pubDate>Sat, 16 Aug 2008 14:18:42 GMT</pubDate>
            <comments>http://geekswithblogs.net/AnneBougie/archive/2008/08/16/recursive-search-for-a-single-file.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/AnneBougie/comments/commentRss/124480.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/AnneBougie/services/trackbacks/124480.aspx</trackback:ping>
        </item>
    </channel>
</rss>
