<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>Anne Bougie</title>
        <link>http://geekswithblogs.net/AnneBougie/Default.aspx</link>
        <description> </description>
        <language>en-US</language>
        <copyright>Anne Bougie</copyright>
        <managingEditor>anne.bougie@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <image>
            <title>Anne Bougie</title>
            <url>http://geekswithblogs.net/images/RSS2Image.gif</url>
            <link>http://geekswithblogs.net/AnneBougie/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>Sucky MSTest and the "WaitAll for multiple handles on a STA thread is not supported" Error</title>
            <category>C# Code</category>
            <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>
            <category>C# Code</category>
            <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>Symantec Customer Dis-Service Department</title>
            <link>http://geekswithblogs.net/AnneBougie/archive/2010/03/01/symantec-customer-dis-service-department.aspx</link>
            <description>&lt;p&gt;What's up with Symantec? My problems with them actually started a couple of months ago when I upgraded to Windows 7. I wanted to install the Norton anti-virus program that I have a subscription for. When I've needed it before, I could go to their site, log in, and re-download my product with no problem. This time, I couldn't find it. I kept being routed to an upgrade screen. I didn't want to upgrade since I was planning on dumping them and going to Kaspersky when my subscription expired anyway. I tried calling their customer support., but gave up after being on hold for about 30 minutes. I just went ahead and bought Kaspersky and figured I'd just cancel my subscription when they sent me a renewal. Well, it came two weeks ago, and I've been trying ever since then to contact them to cancel the subscription with no luck. When you call the customer service number at the bottom of the email, the &amp;lt;very long&amp;gt; message asks if you are calling about a recent charge on your credit card you don't recognize, and about how it's for their award-winning blah blah blah. OK, I'm a little annoyed by having to listen to this announcement which takes over a minute to find out how to reach someone I can talk to. So, I follow the menu commands to get in the right queue and wait..... and wait..... and wait. The recorded message from the overly-perky message-lady voice tells me, "We appreciate your patience, please don't hang up now. If you hang up  and call back now, this will increase your wait time. And all calls are  answered in the order they are received." After about 25 minutes, my cell phone dies. I tried over a period of days at several different times to get a hold of them to no avail. I was starting to look bad at work spending so much time with my phone stuck to my ear. I am a contractor, so I can't really spend hours on personal phone calls at work. Not that it looks any better for employees, but it's worse for contractors. I finally took off of work with the intention of getting this resolved. I made sure my phone had a good charge and called. And waited....and waited.....and waited..... I'm not sure how long I waited. I know it was well over an hour, and closer to two hours. I am wondering at this point if there really are any people there answering the phones. I am picturing this big automated center that holds all these calls with real people whose job is to occasionally interact with someone just to put them in another queue. Heck, they get their money by setting you up on their automatically renewing subscription "service". I think the real service is them serving themselves to your money. At this point, I've given up on trying to get my $45 back from them. I think I will tell my credit card company to cancel my card and issue me a new one so at least next year at this time I won't be stuck with another unwanted charge.&lt;/p&gt; &lt;img src="http://geekswithblogs.net/AnneBougie/aggbug/138260.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Anne Bougie</dc:creator>
            <guid>http://geekswithblogs.net/AnneBougie/archive/2010/03/01/symantec-customer-dis-service-department.aspx</guid>
            <pubDate>Mon, 01 Mar 2010 19:51:36 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/AnneBougie/comments/138260.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/AnneBougie/archive/2010/03/01/symantec-customer-dis-service-department.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/AnneBougie/comments/commentRss/138260.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/AnneBougie/services/trackbacks/138260.aspx</trackback:ping>
        </item>
        <item>
            <title>Using Delegates in Rhino Mocks With the .Do Statement</title>
            <category>C# Code</category>
            <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>
            <category>C# Code</category>
            <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>Part 2: Copy a SharePoint List Programmatically</title>
            <link>http://geekswithblogs.net/AnneBougie/archive/2009/03/15/part-2-copy-a-sharepoint-list-programmatically.aspx</link>
            <description>I started this series to show how to copy different kinds of lists in SharePoint. Each type of list has its own challenges. In this article I will also demonstrate how to copy custom fields and keep the same internal name. It’s kind of annoying that you can’t create a field with the internal name you specify, but I found a pretty easy workaround. You create the field using AddFieldAsXml on the list Fields collection, and you use the internal name as the display name. This creates the field with the correct internal name. Then just change the Title property of the field to what you want displayed. Problem solved. &lt;br style="color: rgb(51, 51, 51);" /&gt;
&lt;br style="color: rgb(51, 51, 51);" /&gt;
Copying a document library is similar to copying a generic list, except that you also need to copy the document file attached to the list item. You also need to determine the template that was used to create the original list, and use that template to create the new list. That way, when users add a new document, it is of the right document type. Another gotcha in copying document libraries, is that there is a subfolder structure that needs to be copied recursively to get everything in its correct place.&lt;br /&gt;
&lt;br /&gt;
Here's the code. I copied it out of my project, which has everything in its own method, so I apologize if some of my capitalization or field names are not correct. Also, there is a function referenced, CopyFieldValues. If you need to know how to do this, refer to Part 1 of this series. Document libraries can have field values just like other lists, so you'll need to copy those too if you want a complete copy of the list.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;// get the document template to use when creating the new list&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;SPDocTemplate docTemplate;&lt;br /&gt;
SPDocTemplateCollection templates = destinationWeb.DocTemplates;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt; XDocument templateSchema = XDocument.Parse(templates.SchemaXml);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt; IEnumerable&amp;lt;XElement&amp;gt; paths = templateSchema.Descendants("DocumentTemplateFile");&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; string sourceUrl = sourceDocumentTemplateUrl.Substring(sourceDocumentTemplateUrl.IndexOf("/") + 1);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt; XElement path = paths.Where(p =&amp;gt; p.Attribute("TargetName").Value == sourceUrl).Single();&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt; XElement docTemplate = path.Parent.Parent;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt; int type = Convert.ToInt32(docTemplate.Attribute("Type").Value);&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; SPDocTemplate destTemplate = null;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt; foreach (SPDocTemplate template in templates)&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 (template.Type == type)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;       docTemplate = template;&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;
Guid newListID = destinationWeb.Lists.Add(sourceList.Title, &lt;br /&gt;
                     sourceList.Description, &lt;br /&gt;
                     destinationWeb.ListTemplates["Document Library"], docTemplate);&lt;br /&gt;
&lt;br /&gt;
SPList destinationList = destinationWeb.Lists[newListID];&lt;br /&gt;
&lt;br /&gt;
// add any custom fields that were created&lt;br /&gt;
foreach (SPField field in SourceList.Fields)&lt;br /&gt;
{&lt;br /&gt;
   if (!DestinationList.Fields.ContainsField(field.InternalName))&lt;br /&gt;
   {&lt;br /&gt;
      // create field with correct internal name&lt;br /&gt;
      XDocument fieldXdoc = XDocument.Parse(field.SchemaXml);&lt;br /&gt;
      XElement fieldElement = fieldXdoc.Element("Field");&lt;br /&gt;
      XAttribute displayName = fieldElement.Attribute("DisplayName");&lt;br /&gt;
      displayName.Value = field.InternalName;&lt;br /&gt;
      string fieldName = DestinationList.Fields.AddFieldAsXml(fieldXdoc.ToString());&lt;br /&gt;
      SPField newfield = DestinationList.Fields.GetField(fieldName);&lt;br /&gt;
      newfield.Title = field.Title;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;&lt;/span&gt;Here is the code for CopyDocumentLibraryItems, which needs to be recursive to get all the items in all the subfolders:&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;br /&gt;
      protected virtual void CopyDocumentLibraryItems(SPFolder sourceFolder, SPFolder destFolder)&lt;br /&gt;
      {&lt;br /&gt;
         // get a list of items in the source folder&lt;br /&gt;
         SPListItemCollection items = ((SPDocumentLibrary)SourceList).GetItemsInFolder(SourceList.DefaultView, sourceFolder);&lt;br /&gt;
&lt;br /&gt;
         foreach (SPListItem item in items)&lt;br /&gt;
         {&lt;br /&gt;
            SPFileSystemObjectType myType = item.FileSystemObjectType;&lt;br /&gt;
            string relativeDestinationUrl = "{0}/{1}";&lt;br /&gt;
            if (myType == SPFileSystemObjectType.File)&lt;br /&gt;
            {&lt;br /&gt;
               SPFile fi = item.File;&lt;br /&gt;
               byte[] filebytes = fi.OpenBinary();&lt;br /&gt;
               relativeDestinationUrl = string.Format(relativeDestinationUrl, destFolder.Url, fi.Name);&lt;br /&gt;
               SPFile newFile = destFolder.Files.Add(relativeDestinationUrl, filebytes, true);&lt;br /&gt;
               SPListItem newItem = newFile.Item;&lt;br /&gt;
               CopyFieldValues(item, newItem); &lt;br /&gt;
               newItem.UpdateOverwriteVersion();&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
               relativeDestinationUrl = string.Format(relativeDestinationUrl, destFolder.Url, item.Folder.Name);&lt;br /&gt;
               SPFolder destSubFolder = destFolder.SubFolders.Add(relativeDestinationUrl);&lt;br /&gt;
               SPFolder sourceSubFolder = item.Folder;&lt;br /&gt;
               // copy everything in sub folder&lt;br /&gt;
               CopyDocumentLibraryItems(sourceSubFolder, destSubFolder);&lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;/span&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;a href="http://technorati.com/tags/SharePoint" rel="tag"&gt;SharePoint&lt;/a&gt;&lt;/div&gt; &lt;img src="http://geekswithblogs.net/AnneBougie/aggbug/130103.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>AnneBougie</dc:creator>
            <guid>http://geekswithblogs.net/AnneBougie/archive/2009/03/15/part-2-copy-a-sharepoint-list-programmatically.aspx</guid>
            <pubDate>Sun, 15 Mar 2009 20:14:38 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/AnneBougie/comments/130103.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/AnneBougie/archive/2009/03/15/part-2-copy-a-sharepoint-list-programmatically.aspx#feedback</comments>
            <slash:comments>8</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/AnneBougie/comments/commentRss/130103.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/AnneBougie/services/trackbacks/130103.aspx</trackback:ping>
        </item>
        <item>
            <title>SharePoint Content Deployment Issue</title>
            <category>SharePoint</category>
            <link>http://geekswithblogs.net/AnneBougie/archive/2009/03/06/sharepoint-content-deployment-issue.aspx</link>
            <description>&lt;p&gt;The following error can occur during content deployment:&lt;/p&gt;
&lt;p&gt; The changeToken refers to a time before the start of the current change log.    &lt;br /&gt;
&lt;br /&gt;
The changeToken refers to a time before the start of the current change log. at Microsoft.SharePoint.Deployment.SPExport.ThrowInvalidChangeTokenError(DateTime minChangeTime, Int32 minChangeNumber) at Microsoft.SharePoint.Deployment.SPExport.GetIncrementalChanges() at Microsoft.SharePoint.Deployment.SPExport.CalculateObjectsToExport() at Microsoft.SharePoint.Deployment.SPExport.Run()&lt;/p&gt;
&lt;p&gt;   Content deployment job 'Entire site' failed.The exception thrown was 'Microsoft.SharePoint.SPException' : 'The changeToken refers to a time before the start of the current change log.'&lt;/p&gt;
&lt;p&gt;This error happens when more than 15 days have passed since the last deployment, and you have the job set to use incremental deployment. The fix is really easy. You just have to change the deployment options on the deployment job to deploy all content instead of deploying only new or changed content. Run the content deployment once, then you can change back to incremental deployment. &lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:d84c7927-f12d-4c80-87b5-3f1f7a0f8210" style="margin: 0px; padding: 0px; display: inline; float: none;"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SharePoint" rel="tag"&gt;SharePoint&lt;/a&gt;&lt;/div&gt; &lt;img src="http://geekswithblogs.net/AnneBougie/aggbug/129880.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>AnneBougie</dc:creator>
            <guid>http://geekswithblogs.net/AnneBougie/archive/2009/03/06/sharepoint-content-deployment-issue.aspx</guid>
            <pubDate>Fri, 06 Mar 2009 15:01:18 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/AnneBougie/comments/129880.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/AnneBougie/archive/2009/03/06/sharepoint-content-deployment-issue.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/AnneBougie/comments/commentRss/129880.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/AnneBougie/services/trackbacks/129880.aspx</trackback:ping>
        </item>
        <item>
            <title>SharePoint Adventures - Reading an Excel Spreadsheet From A Stream</title>
            <category>C# Code</category>
            <category>SharePoint</category>
            <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>Part 1: Copy a SharePoint List Programmatically</title>
            <category>SharePoint</category>
            <link>http://geekswithblogs.net/AnneBougie/archive/2009/01/23/copy-a-sharepoint-list.aspx</link>
            <description>I've been working with the SharePoint API lately. One of my tasks is to copy SharePoint lists from one site to another site. This can be done in code as long as the two sites are on the same machine. It's OK if they are in different web applications, but it can't be from one server to another. If you need to copy from one server to another, you either need to do an export/import, or find a way to save the settings and import them on the new machine. I believe you can use the SharePoint Web Services to accomplish this, though. I haven't worked with that yet. &lt;br /&gt;
&lt;br /&gt;
Anyway, it took a lot of digging around to figure out all the things I needed to copy lists, so I thought I'd post the code that is working for me. This post is only going to cover copying a basic list. I am planning on several more posts to handle document libraries, wikis, and other special cases. Each has their own challenges. &lt;br /&gt;
&lt;br /&gt;
The requirements for me are to copy the bulk of the lists of a large SharePoint site to a new site with a different structure. Therefore, content and deploy is not going to work. Also, they are redesiging the pages, so they really only wanted the lists. What I'm doing is creating a new list on the new site and copying all the properties, so any metadata, such as who last modified the list, is unfortunately lost. &lt;br /&gt;
&lt;br /&gt;
Here's the code:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;span style="color: rgb(51, 153, 102);"&gt;// Instantiate web instances&lt;/span&gt;&lt;br /&gt;
SPSite sourceSite = new SPSite(@"http://SharePointServer:31001");&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;SPWeb sourceWeb = sourceSite.RootWeb;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;SPSite destSite = new SPSite(@"http://SharePointServer:31002");&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;SPWeb destWeb = destSite.RootWeb;&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;&lt;span style="color: rgb(51, 153, 102);"&gt;// Get a reference to the source list&lt;/span&gt;&lt;br /&gt;
SPList sourceList = sourceWeb.GetList("/Lists/Announcements");&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
 &lt;span style="font-family: Courier New;"&gt;&lt;br /&gt;
&lt;span style="color: rgb(51, 153, 102);"&gt;// if the list exists on the destination site, delete it&lt;/span&gt;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;try&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;   SPList temp = destWeb.Lists[sourceList.Title];&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;   destWeb.Lists.Delete(temp.ID);&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;catch { }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;// create new list on the destination web with same properties&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;Guid newListID = destWeb.Lists.Add(sourceList.Title, sourceList.Description,&lt;br /&gt;
                                   sourceList.BaseTemplate);&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;SPList destList = destWeb.Lists[newListID];&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; color: rgb(51, 153, 102);"&gt;// copy items&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;foreach (SPListItem item in announcements.Items)&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;   SPListItem newDestItem = destList.Items.Add();&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;   foreach (SPField field in sourceList.Fields)&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;   {&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;      if (!field.ReadOnlyField)&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;      newDestItem[field.Id] = item[field.Id];&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;   }&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;   newDestItem.Update();&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; color: rgb(51, 153, 102);"&gt;// set quicklaunch settings&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;destList.OnQuickLaunch = sourceList.OnQuickLaunch;&lt;/span&gt;&lt;br style="font-family: Courier New; color: rgb(0, 0, 0);" /&gt;
&lt;span style="font-family: Courier New; color: rgb(0, 0, 0);"&gt;destList.Update();&lt;/span&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&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;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/128937.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>AnneBougie</dc:creator>
            <guid>http://geekswithblogs.net/AnneBougie/archive/2009/01/23/copy-a-sharepoint-list.aspx</guid>
            <pubDate>Fri, 23 Jan 2009 18:18:43 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/AnneBougie/comments/128937.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/AnneBougie/archive/2009/01/23/copy-a-sharepoint-list.aspx#feedback</comments>
            <slash:comments>11</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/AnneBougie/comments/commentRss/128937.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/AnneBougie/services/trackbacks/128937.aspx</trackback:ping>
        </item>
        <item>
            <title>InternalsVisibleTo with Strongly-Type Assemblies</title>
            <category>C# Code</category>
            <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>
    </channel>
</rss>
