title: How to load XML Elements using LINQ from XDocument into a class (not using Descendants)
body: <p>How to load XML Elements using LINQ from XDocument into a c# class. I don't want to use XDocument.Descendants because the settings are not repeated and occur only once in the XML.</p>
  
  <p>This works but I think there must be another way where I don't have to use IEnumerable or use ToArray() and use the first element [0].  Any suggestions?</p>
  
  <p><strong>CODE</strong></p>
  
  <pre><code>public class BackupItem  // class needed so LINQ reads XML into an editable DbGrid
  {
    public bool Backup { get; set; }
    public bool IncludeSubDir { get; set; }
    public string BackupLabel { get; set; }
    public string BackupPath { get; set; }
  }
  
  public class BackupSettings  
  {
    public bool IncludeDateStamp { get; set; }
    public bool DatePrefix { get; set; }
    public bool DateSuffix { get; set; }
    public string DateFormat { get; set; }
    public bool ZipCompress { get; set; }
    public bool ZipPrefix { get; set; }
    public bool ZipSuffix { get; set; }
    public string ZipText { get; set; }
    public bool BackupToSiblingFolder { get; set; }
    public string SiblingFolder { get; set; }
    public bool BackupToFolder { get; set; }
    public bool IncludeFullPath { get; set; }
    public string BackupFolder { get; set; }
  }
  
  
    // use a LINQ query to load xml file into datagridview and make datagrid editable (create class with get/set)
    var q = from arg in GvXMLDoc.Descendants("BackupItem")
            select new BackupItem()
            {
              Backup = (bool)arg.Element("IncludeDirectory"),
              IncludeSubDir = (bool)arg.Element("IncludeSubDirectories"),
              BackupLabel = (string)arg.Element("BackupLabel"),
              BackupPath = (string)arg.Element("BackupPath")
            };
    dataGridView1.DataSource = q.ToList();
  
      // load global variable
      IEnumerable&lt;BackupSettings&gt; GvBackupSettings = from arg in GvXMLDoc.Element("Backup").Elements("Settings")
                                                     select new BackupSettings()
              {
                IncludeDateStamp = (bool)arg.Element("IncludeDateStamp"),
                DatePrefix = (bool)arg.Element("DatePrefix"),
                DateSuffix = (bool)arg.Element("DateSuffix"),
                DateFormat = (string)arg.Element("DateFormat"),
                ZipCompress = (bool)arg.Element("ZipCompress"),
                ZipPrefix = (bool)arg.Element("ZipPrefix"),
                ZipSuffix = (bool)arg.Element("ZipSuffix"),
                ZipText = (string)arg.Element("ZipText"),
                BackupToSiblingFolder = (bool)arg.Element("BackupToSiblingFolder"),
                SiblingFolder = (string)arg.Element("SiblingFolder"),
                BackupToFolder = (bool)arg.Element("BackupToFolder"),
                IncludeFullPath = (bool)arg.Element("IncludeFullPath"),
                BackupFolder = (string)arg.Element("BackupFolder")
              };
  </code></pre>
  
  <p>I can access the values but it seems a very 'messy way'.</p>
  
  <pre><code>var s = GvBackupSettings.ToArray()[0].DateSuffix;
  var t = GvBackupSettings.ToArray()[0].DateFormat;
  </code></pre>
  
  <p><strong>XML FILE</strong></p>
  
  <pre><code>&lt;?xml version='1.0'?&gt;
  &lt;Backup&gt;
    &lt;Settings&gt;
      &lt;IncludeDateStamp&gt;true&lt;/IncludeDateStamp&gt;
      &lt;DatePrefix&gt;false&lt;/DatePrefix&gt;
      &lt;DateSuffix&gt;true&lt;/DateSuffix&gt;
      &lt;DateFormat&gt;yyyy-MM-dd h.m.s&lt;/DateFormat&gt;
      &lt;ZipCompress&gt;false&lt;/ZipCompress&gt;
      &lt;ZipPrefix&gt;false&lt;/ZipPrefix&gt;
      &lt;ZipSuffix&gt;false&lt;/ZipSuffix&gt;
      &lt;ZipText&gt;&lt;/ZipText&gt;
      &lt;BackupToSiblingFolder&gt;true&lt;/BackupToSiblingFolder&gt;
      &lt;SiblingFolder&gt;backups&lt;/SiblingFolder&gt;
      &lt;BackupToFolder&gt;false&lt;/BackupToFolder&gt;
      &lt;IncludeFullPath&gt;true&lt;/IncludeFullPath&gt;
      &lt;BackupFolder&gt;C:\\backup&lt;/BackupFolder&gt;
    &lt;/Settings&gt;
    &lt;BackupItem&gt;
      &lt;IncludeDirectory&gt;true&lt;/IncludeDirectory&gt;
      &lt;IncludeSubDirectories&gt;true&lt;/IncludeSubDirectories&gt;
      &lt;BackupLabel&gt;Backup1&lt;/BackupLabel&gt;
      &lt;BackupPath&gt;C:\TestFiles\Xml\Samples&lt;/BackupPath&gt;
    &lt;/BackupItem&gt;
    &lt;BackupItem&gt;
      &lt;IncludeDirectory&gt;true&lt;/IncludeDirectory&gt;
      &lt;IncludeSubDirectories&gt;false&lt;/IncludeSubDirectories&gt;
      &lt;BackupLabel&gt;Backup2&lt;/BackupLabel&gt;
      &lt;BackupPath&gt;C:\TestFiles\Xml\Samples&lt;/BackupPath&gt;
    &lt;/BackupItem&gt;
  &lt;/Backup&gt;
  </code></pre>
  
  <p><strong>EDIT 1</strong>
  I also am trying to deserialize the XML (thanks for idea) so I don't have to cast each item.  This does not work..  I don't want to have to run the XSD tool either and have a huge class file...</p>
  
  <pre><code>  XmlRootAttribute rootAttribute = new XmlRootAttribute("Backup");
    XmlSerializer deserializer = new XmlSerializer((typeof(BackupSettings)), rootAttribute);
    GvBackupSettings = (BackupSettings)deserializer.Deserialize(XmlReader.Create(GvXMLFileName));
  </code></pre>
  
  <p><strong>EDIT 2</strong>
  Ended up serializing and deserializing xml as suggested below using stardard XSD.exe tool to generate the c# class. Especially as I had to read and write to same xml file.  Note: Make sure you verify/modify the generated xsd file first.  </p>