title: XML parsing exception in C#
body: <p>I'm having a bit of a problem parsing one specific value from an XML document. The code I'm using is as follows:</p>
  
  <pre><code>WebResponse response = request.GetResponse();
  Stream responseStream = response.GetResponseStream();
  XDocument doc = XDocument.Load(responseStream);
  XElement root = doc.Root;
  ClassVars.LastTimeStamp = (int)root.Elements("TIMESTAMP").Last();
  </code></pre>
  
  <p>However, this code is generating the following exception:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.InvalidOperationException'
    occurred in System.Core.dll</p>
  </blockquote>
  
  <p>(the full error message is <a href="http://pastebin.com/sKP4jFZ2" rel="nofollow" title="Pastebin of full error message">here</a>.)</p>
  
  <p>And to be quite frank, I can't figure out for the life of me why. The line which it errors on is the <code>ClassVars.LastTimeStamp = (int)root.Elements("TIMESTAMP").Last();</code> line. I'm trying to parse this from the following XML:</p>
  
  <pre><code>&lt;REGION&gt;
  &lt;MESSAGES&gt;
  &lt;POST&gt;
  &lt;TIMESTAMP&gt;1439137652&lt;/TIMESTAMP&gt;
  &lt;NATION&gt;...&lt;/NATION&gt;
  &lt;MESSAGE&gt;
  &lt;/MESSAGE&gt;
  &lt;/POST&gt;
  ...
  ...
  ...
  &lt;POST&gt;
  &lt;TIMESTAMP&gt;1439137856&lt;/TIMESTAMP&gt;
  &lt;NATION&gt;...&lt;/NATION&gt;
  &lt;MESSAGE&gt;
  ...
  &lt;/MESSAGE&gt;
  &lt;/POST&gt;
  &lt;/MESSAGES&gt;
  &lt;/REGION&gt;
  </code></pre>
  
  <p>What I want to do is extract the TIMESTAMP from the last POST in the file. Can someone tell me what I'm doing wrong? It's probably blindingly obvious, but I just don't see it.</p>