title: How to deserialize XML into an object in C#?
body: <p>I need to read XML and deserialize it into an object in C#. I created the file with the serialization part of the ToXmlFile(Object obj, string filePath) function as given further down. I have the following XML created in the file test.xml:</p>
  
  <pre><code>&lt;NYSE_Daily_Prices&gt;
      &lt;stock_exchange&gt;NYSE&lt;/stock_exchange&gt;
      &lt;stock_symbol&gt;ADI&lt;/stock_symbol&gt;
      &lt;date&gt;2000-01-03T00:00:00&lt;/date&gt;
      &lt;stock_price_open&gt;93.5&lt;/stock_price_open&gt;
      &lt;stock_price_high&gt;93.87&lt;/stock_price_high&gt;
      &lt;stock_price_low&gt;88&lt;/stock_price_low&gt;
      &lt;stock_price_close&gt;90.19&lt;/stock_price_close&gt;
      &lt;stock_volume&gt;3655600&lt;/stock_volume&gt;
      &lt;stock_price_adj_close&gt;39.97&lt;/stock_price_adj_close&gt;
    &lt;/NYSE_Daily_Prices&gt;
    &lt;NYSE_Daily_Prices&gt;
      &lt;stock_exchange&gt;NYSE&lt;/stock_exchange&gt;
      &lt;stock_symbol&gt;ADI&lt;/stock_symbol&gt;
      &lt;date&gt;2000-01-04T00:00:00&lt;/date&gt;
      &lt;stock_price_open&gt;89.5&lt;/stock_price_open&gt;
      &lt;stock_price_high&gt;91.5&lt;/stock_price_high&gt;
      &lt;stock_price_low&gt;85.56&lt;/stock_price_low&gt;
      &lt;stock_price_close&gt;85.62&lt;/stock_price_close&gt;
      &lt;stock_volume&gt;2533200&lt;/stock_volume&gt;
      &lt;stock_price_adj_close&gt;37.95&lt;/stock_price_adj_close&gt;
    &lt;/NYSE_Daily_Prices&gt;
    &lt;NYSE_Daily_Prices&gt;
      &lt;stock_exchange&gt;NYSE&lt;/stock_exchange&gt;
      &lt;stock_symbol&gt;ADI&lt;/stock_symbol&gt;
      &lt;date&gt;2000-01-05T00:00:00&lt;/date&gt;
      &lt;stock_price_open&gt;85.62&lt;/stock_price_open&gt;
      &lt;stock_price_high&gt;88.25&lt;/stock_price_high&gt;
      &lt;stock_price_low&gt;83.19&lt;/stock_price_low&gt;
      &lt;stock_price_close&gt;86.88&lt;/stock_price_close&gt;
      &lt;stock_volume&gt;3228000&lt;/stock_volume&gt;
      &lt;stock_price_adj_close&gt;38.51&lt;/stock_price_adj_close&gt;
    &lt;/NYSE_Daily_Prices&gt;
  </code></pre>
  
  <p>Here is my object:</p>
  
  <pre><code>public partial class NYSE_Daily_Prices
      {
          public string stock_exchange { get; set; }
          public string stock_symbol { get; set; }
          public System.DateTime date { get; set; }
          public double stock_price_open { get; set; }
          public double stock_price_high { get; set; }
          public double stock_price_low { get; set; }
          public double stock_price_close { get; set; }
          public int stock_volume { get; set; }
          public double stock_price_adj_close { get; set; }
      }
  </code></pre>
  
  <p>And this is the code to serialize/deserialize it:</p>
  
  <pre><code>public static class XmlHelper
  {
      public static bool NewLineOnAttributes { get; set; }
      /// &lt;summary&gt;
      /// Serializes an object to an XML string, using the specified namespaces.
      /// &lt;/summary&gt;
      public static string ToXml(object obj, XmlSerializerNamespaces ns)
      {
          Type T = obj.GetType();
  
          var xs = new XmlSerializer(T);
          var ws = new XmlWriterSettings { Indent = true, NewLineOnAttributes = NewLineOnAttributes, OmitXmlDeclaration = true };
  
          var sb = new StringBuilder();
          using (XmlWriter writer = XmlWriter.Create(sb, ws))
          {
              xs.Serialize(writer, obj, ns);
          }
          return sb.ToString();
      }
  
      /// &lt;summary&gt;
      /// Serializes an object to an XML string.
      /// &lt;/summary&gt;
      public static string ToXml(object obj)
      {
          var ns = new XmlSerializerNamespaces();
          ns.Add("", "");
          return ToXml(obj, ns);
      }
  
      /// &lt;summary&gt;
      /// Deserializes an object from an XML string.
      /// &lt;/summary&gt;
      public static T FromXml&lt;T&gt;(string xml)
      {
          XmlSerializer xs = new XmlSerializer(typeof(T));
          using (StringReader sr = new StringReader(xml))
          {
              return (T)xs.Deserialize(sr);
          }
      }
  
      /// &lt;summary&gt;
      /// Deserializes an object from an XML string, using the specified type name.
      /// &lt;/summary&gt;
      public static object FromXml(string xml, string typeName)
      {
          Type T = Type.GetType(typeName);
          XmlSerializer xs = new XmlSerializer(T);
          using (StringReader sr = new StringReader(xml))
          {
              return xs.Deserialize(sr);
          }
      }
  
      /// &lt;summary&gt;
      /// Serializes an object to an XML file.
      /// &lt;/summary&gt;
      public static void ToXmlFile(Object obj, string filePath)
      {
          var xs = new XmlSerializer(obj.GetType());
          var ns = new XmlSerializerNamespaces();
          var ws = new XmlWriterSettings { Indent = true, NewLineOnAttributes = NewLineOnAttributes, OmitXmlDeclaration = true };
          ns.Add("", "");
  
          using (XmlWriter writer = XmlWriter.Create(filePath, ws))
          {
              xs.Serialize(writer, obj);
          }
      }
  
      /// &lt;summary&gt;
      /// Deserializes an object from an XML file.
      /// &lt;/summary&gt;
      public static T FromXmlFile&lt;T&gt;(string filePath)
      {
          StreamReader sr = new StreamReader(filePath);
          try
          {
              var result = FromXml&lt;T&gt;(sr.ReadToEnd());
              return result;
          }
          catch (Exception e)
          {
              throw new Exception("There was an error attempting to read the file " + filePath + "\n\n" + e.InnerException.Message);
          }
          finally
          {
              sr.Close();
          }
      }
  }
  </code></pre>
  
  <p>And the code I use to request the deserialization:</p>
  
  <pre><code>string filePath = $@"C:\Users\dmast\Documents\Upskilled\C#\C# Programming Project\MoneyBMineWpfApp\MoneyBMineWpfApp\OfflineFilesXML\{listRecentSearches.SelectedItem.ToString()}";
              NYSE_Daily_Prices result = XMLReadWrite.XmlHelper.FromXmlFile&lt;NYSE_Daily_Prices&gt;(filePath);
  </code></pre>
  
  <p>I keep getting the caught exception:</p>
  
  <pre><code>catch (Exception e)
                  {
                      throw new Exception("There was an error attempting to read the file " + filePath + "\n\n" + e.InnerException.Message);
              }
  </code></pre>
  
  <p>Any help would be much appreciated :)</p>