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 6255675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:24:35+00:00 2026-05-24T14:24:35+00:00

Im having some trouble with a linq query into an xml tree. Here is

  • 0

Im having some trouble with a linq query into an xml tree. Here is what the tree structure looks like:

<Student>
  <ID> Hello </Hello>
  <Classroom>
     <Name> 1B </Name>
     <Year> 1 </Year>
  </Classroom>
  <Classroom>
     <Name> 2B </Name>
     <Year> 2 </Year>
  </Classroom>
  <Classroom>
     <Name> 3B </Name>
     <Year> 3 </Year>
  </Classroom>
</Student>

Now this is one student entry among 5. say i am passed an XElement Student node from another method and i want to search for a classroom given the XElement Student node and the classroom name. So i have to write a method like this:

getClassRoomNode(XElement StudentNode, string classroomName)

This is what ive tried. Please let me know where i am wrong

XElement classroom = StudentNode.Descendants("Classroom")
                                .Where(arg => arg.Element("Name").Value == classroomName)
                                .Select(arg => arg.Parent)
                                .First();

This returns the StudentNode back again instead of a classroom node. Can anyone please help me out with this?

  • 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-05-24T14:24:36+00:00Added an answer on May 24, 2026 at 2:24 pm

    You don’t need to select parent, just the get the First(), or even better Single() class room.

    XElement classroom = StudentNode.Descendants("Classroom")
        .Single(arg => arg.Element("Name").Value == classroomName);
    

    I suggest it is better to error rather than ignore subsequent matches. If the intention is to match the first of many matches then obviously,

    XElement classroom = StudentNode.Descendants("Classroom")
        .First(arg => arg.Element("Name").Value == classroomName);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some trouble with a LINQ to dataset query. I have the
I'm having trouble getting LINQ to translate something into the query I need. In
I'm having some trouble with a linq query. It's a simple problem, but I'm
I'm having some trouble accessing attributes like DURATION and DATE_MODIFIED of video and image.
I'm having some trouble dealing with linebreaks here. Basically, the user puts in some
Hey having some trouble trying to maintain transparency on a png when i create
I am having some trouble with the Google Maps API . I have an
I'm having some trouble importing and accessing a private key with the ASPNET user.
I'm having some trouble getting log4net to work from ASP.NET 3.5. This is the
I'm having some trouble with plain old JavaScript (no frameworks) in referencing my object

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.