title: Parse XML with LINQ to get child elements
body: <pre><code>&lt;?xml version="1.0" standalone="yes"?&gt;
  &lt;CompanyInfo&gt;
       &lt;Employee name="Jon" deptId="123"&gt;
        &lt;Region name="West"&gt;
          &lt;Area code="96" /&gt;
        &lt;/Region&gt;
        &lt;Region name="East"&gt;
          &lt;Area code="88" /&gt;
        &lt;/Region&gt;
       &lt;/Employee&gt;
  &lt;/CompanyInfo&gt;  
  
  public class Employee
  {
      public string EmployeeName { get; set; }
      public string DeptId { get; set; }
      public List&lt;string&gt; RegionList {get; set;}
  }
  
  public class Region
  {
      public string RegionName { get; set; }
      public string AreaCode { get; set; }
  }
  </code></pre>
  
  <p>I am trying to read this XML data, so far I have tried this:</p>
  
  <pre><code>XDocument xml = XDocument.Load(@"C:\data.xml");
  var xElement = xml.Element("CompanyInfo");
  if (xElement != null)
      foreach (var child in xElement.Elements())
      {
          Console.WriteLine(child.Name);  
          foreach (var item in child.Attributes())
          {
              Console.WriteLine(item.Name + ": " + item.Value);
          }
  
          foreach (var childElement in child.Elements())
          {
              Console.WriteLine("---&gt;" + childElement.Name);
              foreach (var ds in childElement.Attributes())
              {
                  Console.WriteLine(ds.Name + ": " + ds.Value);
              }
              foreach (var element in childElement.Elements())
              {
                  Console.WriteLine("-------&gt;" + element.Name);
                  foreach (var ds in element.Attributes())
                  {
                      Console.WriteLine(ds.Name + ": " + ds.Value);
                  }
              }
          }                
      }
  </code></pre>
  
  <p>This enables me to get each node, its attribute name and value and so I can save these data into the relevant field in database, but this seems a long winded way and 
  not flexible, for instance if the XML structure changes all those foreach statements needs revisiting, also it is difficult to filter the data this way,
  I need to write certain if statements to filter the data (e.g get employees from West only etc...)</p>
  
  <p>I was looking for a more flexible way, using linq,  something like this:</p>
  
  <pre><code>List&lt;Employees&gt; employees =
                (from employee in xml.Descendants("CompanyInfo")
                 select new employee
                 {
                     EmployeeName = employee.Element("employee").Value,
                     EmployeeDeptId = ?? get data,
                     RegionName = ?? get data,
                     AreaCode = ?? get data,,
                 }).ToList&lt;Employee&gt;();
  </code></pre>
  
  <p>But I am not sure how I can get the values from the child nodes and apply the filtering (to get the certain employees only). Is this possible? Any help is appreciated.</p>
  
  <p>Thanks</p>