title: C# Foreach XML node
body: <p>I'm trying to add all the nodes in an XML file into a listView, and I'm doing something wrong but I can't for the life of me figure it out even after looking at a load of examples. This is the XML snippet:</p>
  
  <pre><code>&lt;queue&gt;
  &lt;slots&gt;
  &lt;slot&gt;
  &lt;status&gt;Downloading&lt;/status&gt;
  &lt;filename&gt;file1&lt;/filename&gt;
  &lt;size&gt;1 GB&lt;/size&gt;
  &lt;/slot&gt;
  &lt;slot&gt;
  &lt;status&gt;Downloading&lt;/status&gt;
  &lt;filename&gt;file2&lt;/filename&gt;
  &lt;size&gt;2 GB&lt;/size&gt;
  &lt;/slot&gt;
  &lt;/slots&gt;
  &lt;/queue&gt;
  </code></pre>
  
  <p>And here's the code:</p>
  
  <pre><code>        XDocument xDoc = XDocument.Load(xmlFilePath);
  
          List&lt;Download&gt; list = new List&lt;Download&gt;();
  
          foreach (var download in xDoc.Descendants("slots"))
          {
              string filename = download.Element("filename").Value;
              string size = download.Element("size").Value;
              string status = download.Element("status").Value;
              list.Add(new Download { Filename = filename, Size = size, Status = status });              
          }
  </code></pre>
  
  <p>Any help greatly appreciated as always.</p>
  
  <p>EDIT: To clarify, I'm getting a NullReferenceException on</p>
  
  <pre><code>string filename = download.Element("filename").Value;
  </code></pre>
  
  <p>And i know the listview is missing, I've not done that bit yet :)</p>