You need FirstOrDefault
instead of Select
. Try this in place of Select:
.FirstOrDefault(e => e.Attribute("id") == "665")
Just replace the 665
portion with the id# you want.
EDIT: in looking at StuartLC's answer and the provided xml file. I overlooked the simple design that you used. The data that you are trying to get are indeed elements, not attributes. So you would need to replace the Attribute
call to Element
like so:
.FirstOrDefault(e => e.Element("id") == "665")
On a side note, the return value of Elements
is an IEnumerable<XElement>
which allows you to iterate over the values within it.
EDIT: This will return the XElement
containing the desired data. At this point, you can use StuartLC's answer to transfer the data into your class.