Title: Linq - getting root descendants
Tags: c#;xml;linq
Question: The xml file is this one:
<settings y="1" x="0"&gt;
<prospect aksdj="sdf"&gt;
<image path="images/1.jpg"/&gt;
</prospect&gt;
<prospect aksdfasdj="safafdf"&gt;
<image path="images/2.jpg"/&gt;
</prospect&gt;
</settings&gt;
I want to get both rows with the image tags.
My code is this:
XElement doc = XElement.Load(@"C:\Users\John\Desktop\File.xml");
var result = (from c in doc.Descendants("settings")
select new
{
name = c.Element("prospect").Value
}).ToList();
But, doc.Descendants("settings") is null. Why is it null?
Comment: Your sample XML is broken, btw - the last line should be a closing tag. How representative is it of your real file?

Comment: Sorry, when I shortened the file to put it here I mistakenly erased it :(

Here is the accepted answer: You've loaded an element which is already the <settings&gt; element - that element doesn't have any <settings&gt; descendants. (Descendants isn't returning you null, by the way - it's returning you an empty sequence. There's a big difference.)
If you change it to
XDocument doc = XDocument.Load("...");
then it should be okay - or just load it as an XElement and find the <prospect&gt; descendants, given that you've only got one <settings&gt; element anyway...
Comment for this answer: I found the  descendants and it's ok :)


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:10/27/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><xml><linq>', 'Title': 'Linq - getting root descendants', 'ViewCount': 1050, 'id': 7915166, 'user_AboutMe': '', 'user_AccountId': 938073.0, 'user_DisplayName': 'petko_stankoski', 'user_Reputation': 10127.0, 'user_Views': 1232.0, 'user_id': 966638.0}}