title: Deserialize Xml Using XmlReader and Class from xsd.exe
body: <p>OK I have hit a snag trying to learn XmlSerializer while going through some tutorials.  I have followed all the recommended steps but my program is not returning anything, or is returning null.  I created an XML file as follows:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;WeeklyJobs&gt;
     &lt;DailyJobs Date = "02/03/2012"/&gt;
    &lt;DailyJobs Date = "02/04/2012" TotalJobs = "2"&gt;
      &lt;Jobs&gt;
        &lt;Job JobName = "Job Name" Description = "Description"/&gt;
        &lt;Job JobName = "Job Name" Description = "Description"/&gt;
      &lt;/Jobs&gt;
    &lt;/DailyJobs&gt;
    &lt;DailyJobs Date = "02/05/2012" TotalJobs = "1"&gt;
      &lt;Jobs&gt;
        &lt;Job JobName = "Job Name" Description = "Description"/&gt;
      &lt;/Jobs&gt;
    &lt;/DailyJobs&gt;
    &lt;DailyJobs Date = "02/06/2012" TotalJobs = "2"&gt;
      &lt;Jobs&gt;
        &lt;Job JobName = "Job Name" Description = "Description"/&gt;
        &lt;Job JobName = "Job Name" Description = "Description"/&gt;
      &lt;/Jobs&gt;
    &lt;/DailyJobs&gt;
    &lt;DailyJobs Date = "02/07/2012"/&gt;
    &lt;DailyJobs Date = "02/08/2012" TotalJobs = "2"&gt;
      &lt;Jobs&gt;
        &lt;Job JobName = "Job Name" Description = "Description"/&gt;
        &lt;Job JobName = "Job Name" Description = "Description"/&gt;
      &lt;/Jobs&gt;
    &lt;/DailyJobs&gt;
  &lt;/WeeklyJobs&gt;
  </code></pre>
  
  <p>I then used xsd.exe to generate the .xsd file which is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xs:schema id="WeeklyJobs" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"     xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"&gt;
    &lt;xs:element name="WeeklyJobs" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"&gt;
      &lt;xs:complexType&gt;
        &lt;xs:choice minOccurs="0" maxOccurs="unbounded"&gt;
          &lt;xs:element name="DailyJobs"&gt;
            &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                &lt;xs:element name="Jobs" minOccurs="0" maxOccurs="unbounded"&gt;
                  &lt;xs:complexType&gt;
                    &lt;xs:sequence&gt;
                      &lt;xs:element name="Job" minOccurs="0" maxOccurs="unbounded"&gt;
                        &lt;xs:complexType&gt;
                          &lt;xs:attribute name="JobName" type="xs:string" /&gt;
                          &lt;xs:attribute name="Description" type="xs:string" /&gt;
                        &lt;/xs:complexType&gt;
                      &lt;/xs:element&gt;
                    &lt;/xs:sequence&gt;
                  &lt;/xs:complexType&gt;
                &lt;/xs:element&gt;
              &lt;/xs:sequence&gt;
              &lt;xs:attribute name="Date" type="xs:string" /&gt;
              &lt;xs:attribute name="TotalJobs" type="xs:string" /&gt;
            &lt;/xs:complexType&gt;
          &lt;/xs:element&gt;
        &lt;/xs:choice&gt;
      &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>After creating the schema I used xsd.exe again to auto generate the class for me which is:</p>
  
  <pre><code>/// &lt;remarks/&gt;
  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
  [System.SerializableAttribute()]
  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
  public partial class WeeklyJobs
  {
  
      private WeeklyJobsDailyJobs[] itemsField;
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlElementAttribute("DailyJobs", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
      public WeeklyJobsDailyJobs[] Items
      {
          get
          {
              return this.itemsField;
          }
          set
          {
              this.itemsField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
  [System.SerializableAttribute()]
  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  public partial class WeeklyJobsDailyJobs
  {
  
      private WeeklyJobsDailyJobsJobsJob[][] jobsField;
  
      private string dateField;
  
      private string totalJobsField;
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
      [System.Xml.Serialization.XmlArrayItemAttribute("Job", typeof(WeeklyJobsDailyJobsJobsJob), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
      public WeeklyJobsDailyJobsJobsJob[][] Jobs
      {
          get
          {
              return this.jobsField;
          }
          set
          {
              this.jobsField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string Date
      {
          get
          {
              return this.dateField;
          }
          set
          {
              this.dateField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string TotalJobs
      {
          get
          {
              return this.totalJobsField;
          }
          set
          {
              this.totalJobsField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
  [System.SerializableAttribute()]
  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  public partial class WeeklyJobsDailyJobsJobsJob
  {
  
      private string jobNameField;
  
      private string descriptionField;
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string JobName
      {
          get
          {
              return this.jobNameField;
          }
          set
          {
              this.jobNameField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string Description
      {
          get
          {
              return this.descriptionField;
          }
          set
          {
              this.descriptionField = value;
          }
      }
  }
  </code></pre>
  
  <p>After this I added the .CS file to my project and created a simple winform with a TextBox to display some data once I have deserialized some xml.  Like I said the program launches and nothing is displayed in the TextBox and no Exceptions are thrown.  Here is my winform:</p>
  
  <pre><code>public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
  
      }
  
      public string getExample()
      {
          XmlSerializer serializer = new XmlSerializer(typeof(WeeklyJobs));
          WeeklyJobs jobs;
          string xml = @"&lt;?xml version = ""1.0""?&gt;"
              + @"&lt;WeeklyJobs&gt;"
              + @"&lt;DailyJobs Date = ""02/03/2012""/&gt;"
              + @"&lt;DailyJobs Date = ""02/04/2012"" TotalJobs = ""2""&gt;"
              + @"&lt;Jobs&gt;"
              + @"&lt;Job JobName = ""Job Name"" Description = ""Description""/&gt;"
              + @"&lt;Job JobName = ""Job Name"" Description = ""Description""/&gt;"
              + @"&lt;/Jobs&gt;"
              + @"&lt;/DailyJobs&gt;"
              + @"&lt;/WeeklyJobs&gt;";
  
          // Create an XmlTextReader
          using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
          {
              jobs = (WeeklyJobs)serializer.Deserialize(reader);
          }
  
          return jobs.Items[0].Date;
  
      }
  
      private void Form1_Load(object sender, EventArgs e)
      {
          textBox1.Text = getExample();
  
      }
  
  
  }
  </code></pre>
  
  <p>I have got much more simple xml examples to work but I the second I try to add some complexity to my xml I fail.  I have to figure out how to use the xml I have here. I appreciate the help! Thanks everyone!</p>