I'm having a bit of a problem parsing one specific value from an XML document. The code I'm using is as follows:
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
XDocument doc = XDocument.Load(responseStream);
XElement root = doc.Root;
ClassVars.LastTimeStamp = (int)root.Elements("TIMESTAMP").Last();
However, this code is generating the following exception:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Core.dll
(the full error message is here.)
And to be quite frank, I can't figure out for the life of me why. The line which it errors on is the ClassVars.LastTimeStamp = (int)root.Elements("TIMESTAMP").Last(); line. I'm trying to parse this from the following XML:
<REGION>
<MESSAGES>
<POST>
<TIMESTAMP>1439137652</TIMESTAMP>
<NATION>...</NATION>
<MESSAGE>
</MESSAGE>
</POST>
...
...
...
<POST>
<TIMESTAMP>1439137856</TIMESTAMP>
<NATION>...</NATION>
<MESSAGE>
...
</MESSAGE>
</POST>
</MESSAGES>
</REGION>
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.