title: Using XMLSerializer for reading/saving settings file in WPF app
body: <p>I have an application that I'm trying to implement the ability to load and save settings with.  I got some sample code from another SO user's question and it half works, but I'm not sure what's going wrong.</p>
  
  <p>I'm using XmlSerializer to save the AppSettings object to a file in the code below, and also to read the file back to the object.  In the event that there is no file then it sets defaults and saves them before returning the object to the main application.</p>
  
  <pre><code>public class AppSettings
  {
      static string applicationPath = AppDomain.CurrentDomain.BaseDirectory;
      string settingsFile = applicationPath + "Settings.xml";
      internal string databaseName;
  
      public void Save()
      {
          try
          {
              using (StreamWriter writer = new StreamWriter(settingsFile))
              {
                  XmlSerializer serializer = new XmlSerializer(typeof(AppSettings));
                  serializer.Serialize(writer, this);
              }
          }
          catch
          {
          }
      }
  
      public AppSettings Read()
      {
          if (File.Exists(settingsFile))
          {
              try
              {
                  using (StreamReader reader = new StreamReader(settingsFile))
                  {
                      XmlSerializer deserializer = new XmlSerializer(typeof(AppSettings));
                      return deserializer.Deserialize(reader) as AppSettings;
                  }
              }
              catch
              {
                  return GetDefaults();
              }
          }
          else
          {
              return GetDefaults();
          }
  
      }
  
      public AppSettings GetDefaults()
      {
          databaseName = "DBNAME";
          Save();
          return this;
      }
  }
  </code></pre>
  
  <p>I'm not getting any errors and a Settings.xml file is being generated, but the databaseName object doesn't appear to be being saved. Reading the file seems to work fine as well but the databaseName value is null.  Checking the xml file I can't see any data except the xml metadata tags so I'm assuming there's a mistake with my approach to saving the AppSettings object in the first place.</p>
  
  <p>XML file contents:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;AppSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /&gt;
  </code></pre>