Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:58:05+00:00 2026-06-17T16:58:05+00:00

Question regarding nested LINQ to XML queries: code: List<string> GetNames(string fooName) { string fileName

  • 0

Question regarding nested LINQ to XML queries:

code:

List<string> GetNames(string fooName)
{
  string fileName = "foo.xml";

  XElement myFile = XElement.Load(fileName);
  IEnumerable<XElement> bars =
     from response in myFile.Elements("foo")
     where response.Attribute("fooName").Value.Equals(fooName)
     from subResponse in response.Descendants()
     select subResponse;

  List<string> sNames = new List<string>();

  foreach (XElement el in bars)
  {
     XAttribute NameAttr = el.Attribute("Name");
     sNames.Add(NameAttr.Value);
  }
  return sNames;
}

xml:

<topElem>
  <foo fooname="a">
     <bar Name= "bb"/>
     <bar Name= "cc"/>
     <bar Name= "dd"/>
  </foo>
</topElem>

question:

What I am trying to do is build a list that has “bb”, “cc”, and “dd” in it, when I called the above function with an argument of “a”. While the above code works, I think if I was more fluent in LINQ I would be able to produce my List directly in the LINQ code and not have to rely on the foreach loop to iterate over the IEnumerable. I’ve looked at a number of examples on this site and others trying to find this exact case, but haven’t had much luck. I’ve seen some examples that use lambda expressions or that do other operations on the “select” statement, but I haven’t had any luck getting code which compiles out of that. Maybe adding a 3rd from statement, something like

from subSubResponse in subResponse.Attribute("Name")
select subResponse.Value

But that gives an error:

An expression of type ‘System.Xml.Linq.XAttribute’ is not allowed in a subsequent from clause in a query expression with source type ‘System.Collections.Generic.IEnumerable’. Type inference failed in the call to ‘SelectMany’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:58:06+00:00Added an answer on June 17, 2026 at 4:58 pm

    You can only use from on an expression that returns a collection.
    .Attribute("Name") does not return a collection, so that line doesn’t make sense.

    You may want

    let subSubResponse = subResponse.Attribute("Name")
    

    or just

    select subResponse.Attribute("Name").Value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently asked a question regarding how to Save a list with nested elements
I have a question regarding applying a RTRIM on a ASP:Hyperlink statement. The code
I have a question regarding nested doseq loops. In the start function, once I
I have a REALLY BASIC question regarding nested divs and height inheritance. Let's say
I'm learning about Knockout.js and I've a question regarding updating nested data, is what
My question is about Python List Comprehension readability. When I come across code with
Question regarding GtkBuilder. When we unref builder pointer does it destroys all the screens/widgets
Question regarding fluent nHibernate table mappings: Apparently I was under the mistaken impression, that
Quick question regarding memory management in C++ If I do the following operation: pointer
simple question regarding property value inheritance in UserControls. If i create a UserControl, it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.