title: Complex/Nested XML Reading in C#
body: <p>I have been trying to read this XML file however it is complex/nested a good amount compared to the examples I have seen online. I have tried using LINQ and XMLReader with no luck.</p>
  <p>LINQ will read each OrderScreen; however, when it comes to the Cell of each OrderScreen it loads all possible Cells into each OrderScreen even if the Cell does not belong to that OrderScreen. I understand why it does it, but I am fairly new to LINQ and most of the examples I see are not this complex and do not really cover this.</p>
  <p>XMLReader works pretty well but it does not continue reading the next Cell after it completed the reading of one OrderScreen, it just reads the first Cell of the next OrderScreen then assumes it is at the end of the document. I did not include that code because all the searches I have seen people using LINQ over XMLReader.</p>
  <p>XML is below first, most recent LINQ code after that</p>
  <p>Any help is greatly appreciated!</p>
  <pre><code>&lt;Screens&gt;
    &lt;DeleteScreens&gt;&lt;/DeleteScreens&gt;
    &lt;NewScreens&gt;
      &lt;OrderScreen&gt;
        &lt;ScreenNumber&gt;&lt;/ScreenNumber&gt;
        &lt;Title&gt;&lt;/Title&gt;
        &lt;NumberOfColumns&gt;&lt;/NumberOfColumns&gt;
        &lt;OptionScreen&gt;&lt;/OptionScreen&gt;
        &lt;ShowQuantityButtons&gt;&lt;/ShowQuantityButtons&gt;
        &lt;PrepSequenceScreen&gt;&lt;/PrepSequenceScreen&gt;
        &lt;Cell&gt;
          &lt;CellNumber&gt;&lt;/CellNumber&gt;
          &lt;CellName&gt;&lt;/CellName&gt;
          &lt;InventoryNumber&gt;&lt;/InventoryNumber&gt;
          ...more Cell elements..
          &lt;OptionGroup&gt;
            &lt;Type&gt;&lt;/Type&gt;
            &lt;ScreenNumber&gt;&lt;/ScreenNumber&gt;
            &lt;Cells&gt;&lt;/Cells&gt;
          &lt;/OptionGroup&gt;
          ...more OptionGroups...
        &lt;/Cell&gt;
        ...more Cells...
      &lt;/OrderScreen&gt;
      ...more OrderScreens...
    &lt;/NewScreens&gt;
    &lt;UpdateMenus&gt;
       &lt;Menu&gt;
        &lt;MenuNumber&gt;&lt;/MenuNumber&gt;
        &lt;MenuTitle&gt;&lt;/MenuTitle&gt;
        ...more Menu elements...
      &lt;/Menu&gt;
      ...more Menus...
    &lt;/UpdateMenus&gt;
  &lt;Screens&gt;
  
  XDocument xdoc;
  xdoc = XDocument.Load(@&quot;C:\Users\Kwagstaff\Desktop\PMM_3.0\PMM_3.0\XML\Screens.xml&quot;);
  var ORDERSCREENS = from a in xdoc.Descendants(&quot;OrderScreen&quot;)
   select new
   {
    ScreenNumber = a.Element(&quot;ScreenNumber&quot;).Value,
    Title = a.Element(&quot;Title&quot;).Value,
    NumberOfColumns = a.Element(&quot;NumberOfColumns&quot;).Value,
    OptionScreen = a.Element(&quot;OptionScreen&quot;).Value,
    ShowQuantityButtons = a.Element(&quot;ShowQuantityButtons&quot;).Value,
    PrepSequenceScreen = a.Element(&quot;PrepSequenceScreen&quot;).Value,
    Cell = from b in xdoc.Descendants(&quot;Cell&quot;)
    select new
    {
     CellNumber = b.Element(&quot;CellNumber&quot;),
    }   
  };
  </code></pre>