title: System.InvalidOperationException was unhandled - XMl Serialization C#
body: <p>I made an xml document by using XML Serialization.</p>
  
  <p>It looks like this</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Course xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;courseName&gt;Comp 1510&lt;/courseName&gt;
    &lt;backgroundColor&gt;#ffffff&lt;/backgroundColor&gt;
    &lt;fontColor&gt;#ffffff&lt;/fontColor&gt;
    &lt;sharingKey&gt;ed35d1f8-6be1-4f87-b77f-c70298e5abbb&lt;/sharingKey&gt;
    &lt;task type="Assignment"&gt;
      &lt;taskName&gt;First Task&lt;/taskName&gt;
      &lt;description&gt;description&lt;/description&gt;
      &lt;taskDueDate&gt;2010-01-24T12:41:20.0321826-08:00&lt;/taskDueDate&gt;
      &lt;weight xsi:nil="true" /&gt;
      &lt;beforeDueDateNotification&gt;30&lt;/beforeDueDateNotification&gt;
      &lt;outOf&gt;50.4&lt;/outOf&gt;
    &lt;/task&gt;
  &lt;/Course&gt;
  </code></pre>
  
  <p>My Code to make this</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Xml.Serialization;
  
  namespace ConsoleApplication1
  {
      [XmlRoot("Course")]
      public class MyWrapper 
      {
          public MyWrapper()
          {
              TaskList = new List&lt;Tasks&gt;();
          }
  
          [XmlElement("courseName")]
          public string CourseName { get; set; }
  
          [XmlElement("backgroundColor")]
          public string BackgroundColor { get; set; }
  
          [XmlElement("fontColor")]
          public string  FontColor { get; set; }
  
          [XmlElement("sharingKey")]
          public Guid SharingKey { get; set; }
  
          [XmlElement("task")]
          public List&lt;Tasks&gt; TaskList { get; set; }
  
      }
  
  public class Tasks
  {
      [XmlAttribute("type")]
      public string Type { get; set; }
  
      [XmlElement("taskName")]
      public string TaskName { get; set; }
  
      [XmlElement("description")]
      public string Description { get; set; }
  
      [XmlElement("taskDueDate")]
      public DateTime TaskDueDate { get; set; }
  
      [XmlElement("weight")]
      public decimal? Weight { get; set; }
  
      [XmlElement("beforeDueDateNotification")]
      public int BeforeDueDateNotification { get; set; }
  
      [XmlElement("outOf")]
      public decimal? OutOf { get; set; }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Xml.Serialization;
  using System.IO;
  
  namespace ConsoleApplication1
  {
      class Program
      {
          static void Main(string[] args)
          {
  
              MyWrapper wrap = new MyWrapper();
              wrap.CourseName = "Comp 1510";
              wrap.FontColor = "#ffffff";
              wrap.BackgroundColor = "#ffffff";
              wrap.SharingKey = Guid.NewGuid();
  
              Tasks task = new Tasks()
              {
                  TaskName = "First Task",
                  Type = "Assignment",
                  TaskDueDate = DateTime.Now,
                  Description = "description",
                  BeforeDueDateNotification = 30,
                  OutOf = 50.4M
              };
  
              wrap.TaskList.Add(task);
              SerializeToXML(wrap);
  
             var grab = DeserializeFromXML();
  
             foreach (var item in grab)
             {
  
             }
          }
  
          static public void SerializeToXML(MyWrapper list)
          {
              XmlSerializer serializer = new XmlSerializer(typeof(MyWrapper));
              TextWriter textWriter = new StreamWriter(@"C:\New folder\test.xml");
              serializer.Serialize(textWriter, list);
              textWriter.Close();
          }
  
          static List&lt;MyWrapper&gt; DeserializeFromXML()
          {
              XmlSerializer deserializer = new XmlSerializer(typeof(List&lt;MyWrapper&gt;));
              TextReader textReader = new StreamReader(@"C:\New folder\test.xml");
              List&lt;MyWrapper&gt; tasks;
              tasks = (List&lt;MyWrapper&gt;)deserializer.Deserialize(textReader);
              textReader.Close();
  
              return tasks;
          }
      }
  }
  </code></pre>
  
  <p>Now when I try to de serialize it I get this error</p>
  
  <pre><code>System.InvalidOperationException was unhandled
    Message="There is an error in XML document (2, 2)."
    Source="System.Xml"
    StackTrace:
         at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
         at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
         at ConsoleApplication1.Program.DeserializeFromXML() in C:\Users\chobo2\Desktop\ConsoleApplication1\ConsoleApplication1\Program.cs:line 55
         at ConsoleApplication1.Program.Main(String[] args) in C:\Users\chobo2\Desktop\ConsoleApplication1\ConsoleApplication1\Program.cs:line 34
         at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
         at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         at System.Threading.ThreadHelper.ThreadStart()
    InnerException: System.InvalidOperationException
         Message="&lt;Course xmlns=''&gt; was not expected."
         Source="ap72r7cf"
         StackTrace:
              at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderList1.Read5_ArrayOfMyWrapper()
         InnerException: 
  </code></pre>
  
  <p>I am not sure why this is happening. </p>
  
  <p><strong>Some Side Questions - Answer please after main problem is answered</strong></p>
  
  <p>I did not want to make a new forum post for these 2 questions unless I have to.</p>
  
  <ol>
  <li><p>Why is it important to give it an object type? Like why not make all the fields as strings?</p>
  
  <pre><code>    [XmlElement("sharingKey")]
      public Guid SharingKey { get; set; }
  </code></pre></li>
  </ol>
  
  <p>Is it just for when you de serialize that you will get in this case a Guid so that you won't later one have to convert it from a string to a Guid?</p>
  
  <p>If this is correct how about if you get an xml file from someone else and you want to de serialize it how will you know what objects will come out of it? Like for instance how would you know that my "OutOf" is actually a type of nullable decimal? In fact how does C# know that? I don't see anything that would tip it off that this is the type.</p>
  
  <ol start="2">
  <li>When I get it to actually de serialize, I am wondering how do I make my foreach loop. Since I want to go through each of the list of "MyWrapper" objects. But in MyWrapper there is a collection of Task objects. So do I have to make a for loop inside my foreach loop to get at it? Or is there a better way?</li>
  </ol>
  
  <p>Thanks</p>