title: XmlSerializer/Deserialize in C#
body: <p>I created this class where it'll append to the XML called Email Alerts h1, body and ids. However, im having trouble running this file from the start (when i call LoadFromFile), when the file does not exist - so we'll have to create a file and start logging it.</p>
  
  <pre><code>public class EMailAlert
  {
      public string h1 { get; set; }
      public string body { get; set; }
      public string idList { get; set; }
  
      public void Save(string fileName)
      {
          using (var stream = new FileStream(fileName, FileMode.Create))
          {
              var XML = new XmlSerializer(typeof(EMailAlert));
              XML.Serialize(stream, this);
          }
      }
  
      public static EMailAlert LoadFromFile(string fileName)
      {
          if (!File.Exists(fileName))
          {
              var file = new FileInfo(fileName);
              file.Directory.Create();
              var xmlFile = new XDocument(
              new XDeclaration("1.0", "utf-8", "yes"),
              new XComment("Email Logger"));
              xmlFile.Add(new XElement("EmailAlerts"));
              xmlFile.Save(fileName);
          }
          using (var stream = new FileStream(fileName, FileMode.Open))
          {
              var XML = new XmlSerializer(typeof(EMailAlert));
              return (EMailAlert)XML.Deserialize(stream);
          }
      }
  }
  </code></pre>
  
  <p>When i run this code (there are no xml file - so it creates an XML file and then it throws this error <code>{"&lt;EmailAlerts xmlns=''&gt; was not expected."}</code></p>
  
  <p>Here is how the xml file looks like </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="yes"?&gt;
  &lt;!--Email Logger--&gt;
  &lt;EmailAlerts /&gt;
  </code></pre>
  
  <p>Not sure why its not sending back an empty EmailAlert when i call LoadFromFile.</p>