4

I am doing the following query on an XDocument. The last level .Descendants("Instance") yields a list of XElements of the form:

<Instance>filepath1</Instance>
<Instance>filepath2</Instance>
<Instance>filepath3</Instance>
<Instance>filepath4</Instance>

Query

 List<string> fileNames = xDoc.Descendants("Main")
                       .FirstOrDefault()
                       .Descendants("SecondLevel")
                       .FirstOrDefault()
                       .Descendants("Instance")
                       .Select().ToList(); //this line is not correct. Need to get the instances node values as List<string>

How can I store the values filepath1, filepath2.. in the List<string>?

1
  • 1
    Something like Select(x=>x.Value) ? Commented Mar 6, 2012 at 10:19

1 Answer 1

6

By using

 ....
  .Descendants("Instance")
  .Select(e => e.Value) // project to the string values
  .ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it worked. Forgive my ignorance, what is that syntax. It looks like writing anonymous functions with Lamda expressions.
It is a lambda expression. It Converts the found XElements into their string Values. Read up on basic LINQ, any tutorial will do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.