title: Select multiple elements with Linq to XML
body: <p>I have a C# application and need to extract multiple elements from a Linq to XML collection.</p>
  
  <p>I have the following extract from an XML file</p>
  
  <pre><code>    &lt;SNS&gt;
      &lt;uniqueSystem&gt;&lt;system&gt;49&lt;/system&gt;&lt;label&gt;Engines&lt;/label&gt;
          &lt;uniqueSubsystem&gt;&lt;subsystem&gt;30&lt;/subsystem&gt;&lt;label&gt;APU&lt;/label&gt;
              &lt;uniqueUnit&gt;&lt;unit&gt;00&lt;/unit&gt;&lt;label&gt;Starter&lt;/label&gt;
      &lt;/uniqueUnit&gt;
      &lt;/uniqueSubsystem&gt;
      &lt;/uniqueSystem&gt;
      &lt;uniqueSystem&gt;&lt;system&gt;50&lt;/system&gt;&lt;label&gt;Hydraulics&lt;/label&gt;
          &lt;uniqueSubsystem&gt;&lt;subsystem&gt;30&lt;/subsystem&gt;&lt;label&gt;Reservoir&lt;/label&gt;
              &lt;uniqueUnit&gt;&lt;unit&gt;00&lt;/unit&gt;&lt;label&gt;Pump&lt;/label&gt;
      &lt;/uniqueUnit&gt;
      &lt;/uniqueSubsystem&gt;
  &lt;/uniqueSystem&gt;&lt;/SNS&gt;
  </code></pre>
  
  <p>I need to extract the values from within each 'uniqueSystem' element. So in the example above, under the 'SNS' element there are 2 'uniqueSystem' elements, and within each of these there are 'uniqueSubsystem' elements and 'uniqueUnit' elements each with 'label' elements. I need to extract this data to build a TreeView.</p>
  
  <p>My problem is extracting multiple elements using Linq. How do i do this?</p>
  
  <p>At the moment i have </p>
  
  <pre><code>var item = from items in doc.Descendants("SNS").Descendants("uniqueSystem").Descendants("system")
                            orderby items.Value
                            select items.Descendants("uniqueSystem");
  </code></pre>
  
  <p>I think this will give me a collection of the 'uniqueSystem' elements, from which i now need to extract the values of the multiple elements within. Can anybody please help?</p>
  
  <p>My next attmpt is as follows, but this is giving me a null reference exception:</p>
  
  <pre><code>var item = from items in doc.Descendants("SNS").Descendants("uniqueSystem").Descendants("system")
                            orderby items.Value
                            select items.Descendants("uniqueSystem");
  
                  foreach (var e in item)
                  {
                      string sys = e.Descendants("system").FirstOrDefault().Value;
                      string sysLabel = e.Descendants("system").Descendants("label").FirstOrDefault().Value;
  
                      string subsys = e.Descendants("subsystem").FirstOrDefault().Value;
                      string subsysLabel = e.Descendants("subsystem").Descendants("label").FirstOrDefault().Value;
  
                      string unit = e.Descendants("unit").FirstOrDefault().Value;
                      string unitLabel = e.Descendants("unit").Descendants("label").FirstOrDefault().Value;
  
                      buildSystemNodes(sys, sysLabel);
                      //getSubSystems(myitem);
                  }
  </code></pre>