title: Loop xml and store values as variables - C#
body: <p>New to C# here and I would like some help with a small project of mine. I have an XML file with elements that I would like to loop through and store it´s values and use.</p>
  <p>I want to grab all of the descending attributes in &quot;week 2&quot; and store them as variables and execute some code. Than I would like to do the process all over again with the next descending elements of the next element &quot;week 2&quot;.</p>
  <p>Can any help me out?</p>
  <p>My C# code so far:</p>
  <pre><code>XDocument xdoc = XDocument.Load(IHCSchedulingConfigFile);
  
  var xelem = xdoc.Descendants(&quot;week&quot;)
                  .Where(x =&gt; x.Attribute(&quot;weeknumber&quot;).Value.Equals(&quot;2&quot;))
                  .ToList();
  
  foreach (var week in xelem)
  {
      int groupNumber = Convert.ToInt32(xelem.Elements(&quot;groupNumber&quot;).Any());
      string starTtime = xelem.Elements(&quot;starttime&quot;).ToString();
      int startDay = Convert.ToInt32(xelem.Elements(&quot;startday&quot;).Any());
      string endTime = xelem.Elements(&quot;endtime&quot;).ToString();
      int endDay = Convert.ToInt32(xelem.Elements(&quot;endday&quot;).Any());
  
      Console.WriteLine(groupNumber);
      Console.WriteLine(&quot;\r\n&quot;);
      Console.WriteLine(starTtime);
      Console.WriteLine(&quot;\r\n&quot;);
      Console.WriteLine(startDay);
      Console.WriteLine(&quot;\r\n&quot;);
      Console.WriteLine(endTime);
      Console.WriteLine(&quot;\r\n&quot;);
      Console.WriteLine(endDay);
      Console.WriteLine(&quot;\r\n&quot;);
  
      Console.ReadLine();
  
      // Execute code 
  }
  </code></pre>
  <p>XML to read values from:</p>
  <pre><code>&lt;root&gt;
      &lt;week weeknumber=&quot;1&quot;&gt;
          &lt;groupNumber&gt;1&lt;/groupNumber&gt;
          &lt;starttime&gt;10:00&lt;/starttime&gt;
          &lt;startday&gt;8&lt;/startday&gt;
          &lt;endtime&gt;12:00&lt;/endtime&gt;
          &lt;endday&gt;8&lt;/endday&gt;
      &lt;/week&gt; 
      &lt;week weeknumber=&quot;1&quot;&gt;
          &lt;groupNumber&gt;2&lt;/groupNumber&gt;
          &lt;starttime&gt;10:00&lt;/starttime&gt;
          &lt;startday&gt;8&lt;/startday&gt;
          &lt;endtime&gt;12:00&lt;/endtime&gt;
          &lt;endday&gt;8&lt;/endday&gt;
      &lt;/week&gt;
      &lt;week weeknumber=&quot;2&quot;&gt;
          &lt;groupNumber&gt;1&lt;/groupNumber&gt;
          &lt;starttime&gt;10:00&lt;/starttime&gt;
          &lt;startday&gt;8&lt;/startday&gt;
          &lt;endtime&gt;12:00&lt;/endtime&gt;
          &lt;endday&gt;8&lt;/endday&gt;
      &lt;/week&gt; 
      &lt;week weeknumber=&quot;2&quot;&gt;
          &lt;groupNumber&gt;2&lt;/groupNumber&gt;
          &lt;starttime&gt;10:00&lt;/starttime&gt;
          &lt;startday&gt;8&lt;/startday&gt;
          &lt;endtime&gt;12:00&lt;/endtime&gt;
          &lt;endday&gt;8&lt;/endday&gt;
      &lt;/week&gt;
      &lt;week weeknumber=&quot;3&quot;&gt;
          &lt;groupNumber&gt;1&lt;/groupNumber&gt;
          &lt;starttime&gt;10:00&lt;/starttime&gt;
          &lt;startday&gt;8&lt;/startday&gt;
          &lt;endtime&gt;12:00&lt;/endtime&gt;
          &lt;endday&gt;8&lt;/endday&gt;
      &lt;/week&gt; 
      &lt;week weeknumber=&quot;3&quot;&gt;
          &lt;groupNumber&gt;2&lt;/groupNumber&gt;
          &lt;starttime&gt;10:00&lt;/starttime&gt;
          &lt;startday&gt;8&lt;/startday&gt;
          &lt;endtime&gt;12:00&lt;/endtime&gt;
          &lt;endday&gt;8&lt;/endday&gt;
      &lt;/week&gt;
  &lt;/root&gt;
  </code></pre>