title: Use XML stored in a resource file as mock data for unit tests
body: <p>I'm trying to use some XML data stored in a .resx file as test data for unit tests.</p>
  
  <p>I have this code that takes the test data, converts it to a stream, and tries to deserialize it into a collection of Students that I can use with my mock dbset.</p>
  
  <pre><code>var stream = TestData.Students.ToStream();
  var reader = new StreamReader(stream);
  var serializer = new XmlSerializer(typeof(Collection&lt;Student&gt;));
  _students = (Collection&lt;Student&gt;)serializer.Deserialize(reader);
  </code></pre>
  
  <p>Student class</p>
  
  <pre><code>public class Student
  {
      public int Id { get; set; }
      public string LastName { get; set; }
      public string FirstName { get; set; }
      public char MiddleInitial { get; set; }
      public DateTime? EnrollmentDate { get; set; }
  
      [XmlIgnore]
      public virtual ICollection&lt;Enrollment&gt; Enrollments { get; set; }
  }
  </code></pre>
  
  <p>It fails on the last line with this exception:</p>
  
  <pre><code>{"&lt;Students xmlns=''&gt; was not expected."}
  </code></pre>
  
  <p>I've tried adding the Serializable attribute to the Student class, tried adding the XMLRoot attribute to it, also searched around on Google for a while, but couldn't really find anything dealing with resource files.</p>
  
  <p>On a side note, is there an easier/better method for getting test data?</p>
  
  <p><b>Edit</b>
  If it helps I'm following <a href="http://www.danylkoweb.com/Blog/the-fastest-way-to-mock-a-database-for-unit-testing-B6" rel="nofollow">this</a> article.</p>
  
  <p><b>Edit 2</b>
  So drilling down into the stack trace of the inner exception, I found this line:</p>
  
  <pre><code>at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderCollection1.Read4_ArrayOfStudent()
  </code></pre>
  
  <p>So I changed the root element of the XML from Students to ArrayOfStudent and voila, it correctly deserializes.</p>
  
  <p>Here's my XML data in the resource file.</p>
  
  <pre><code>&lt;Students&gt;
    &lt;Student&gt;
      &lt;Id&gt;1&lt;/Id&gt;
      &lt;LastName&gt;Alexander&lt;/LastName&gt;
      &lt;FirstName&gt;Carson&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2010-09-01T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;2&lt;/Id&gt;
      &lt;LastName&gt;Alonso&lt;/LastName&gt;
      &lt;FirstName&gt;Meredith&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2012-09-01T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;3&lt;/Id&gt;
      &lt;LastName&gt;Anand&lt;/LastName&gt;
      &lt;FirstName&gt;Arturo&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2013-09-01T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;4&lt;/Id&gt;
      &lt;LastName&gt;Barzdukas&lt;/LastName&gt;
      &lt;FirstName&gt;Gytis&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2012-09-01T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;5&lt;/Id&gt;
      &lt;LastName&gt;Li&lt;/LastName&gt;
      &lt;FirstName&gt;Yan&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2012-09-01T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;6&lt;/Id&gt;
      &lt;LastName&gt;Justice&lt;/LastName&gt;
      &lt;FirstName&gt;Peggy&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2011-09-01T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;7&lt;/Id&gt;
      &lt;LastName&gt;Norman&lt;/LastName&gt;
      &lt;FirstName&gt;Laura&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2013-09-01T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;8&lt;/Id&gt;
      &lt;LastName&gt;Olivetto&lt;/LastName&gt;
      &lt;FirstName&gt;Nino&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2005-08-11T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;9&lt;/Id&gt;
      &lt;LastName&gt;Alexander&lt;/LastName&gt;
      &lt;FirstName&gt;Carson&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2010-09-01T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;10&lt;/Id&gt;
      &lt;LastName&gt;Alonso&lt;/LastName&gt;
      &lt;FirstName&gt;Meredith&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2012-09-01T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;11&lt;/Id&gt;
      &lt;LastName&gt;Anand&lt;/LastName&gt;
      &lt;FirstName&gt;Arturo&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2013-09-01T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;12&lt;/Id&gt;
      &lt;LastName&gt;Barzdukas&lt;/LastName&gt;
      &lt;FirstName&gt;Gytis&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2012-09-01T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;13&lt;/Id&gt;
      &lt;LastName&gt;Li&lt;/LastName&gt;
      &lt;FirstName&gt;Yan&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2012-09-01T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;14&lt;/Id&gt;
      &lt;LastName&gt;Justice&lt;/LastName&gt;
      &lt;FirstName&gt;Peggy&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2011-09-01T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;15&lt;/Id&gt;
      &lt;LastName&gt;Norman&lt;/LastName&gt;
      &lt;FirstName&gt;Laura&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2013-09-01T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
    &lt;Student&gt;
      &lt;Id&gt;16&lt;/Id&gt;
      &lt;LastName&gt;Olivetto&lt;/LastName&gt;
      &lt;FirstName&gt;Nino&lt;/FirstName&gt;
      &lt;EnrollmentDate&gt;2005-08-11T00:00:00&lt;/EnrollmentDate&gt;
    &lt;/Student&gt;
  &lt;/Students&gt;
  </code></pre>