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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:05:51+00:00 2026-05-11T19:05:51+00:00

I have the following XML Document being loaded into C# Silverlight: <parent> <son name=Jim>

  • 0

I have the following XML Document being loaded into C# Silverlight:

<parent>
    <son name="Jim">
        <grandson>Billy</grandson>
        <granddaughter>Sue</granddaughter>
    </son>
    <daughter name="Sally">
    </daughter> 
</parent>

I’d like to do a LINQ query so that I query parent and get a list of “son” and “daughter” nodes only. When I get to a node of type “son”, I want to do another query for its own children.

I’ve tried this:

 IEnumerable<XElement> Children =
                    from childNode in parents.Descendants()
                    select (XElement)childNode ;

foreach(XElement childNode in Children){
// other code 
}

but that gives me everything (son, daughter, grandson, granddaughter).

What I’d like to do is something like this:

  foreach(XElement childNode in Children){
    switch(childNode.Name.ToString()){
         case "son": 
            // look for "grandson" and "granddaughter" as children of "son" now
            break;
        case "daughter":
           // don't look for anything
           break;
        }
    }

So Basically, I only want the first level of children returned in the query, and I will query for the rest later on.

I’m not sure if it should be done in the original LINQ query, in the foreach condition, or what. I don’t have control over the XML document format so I can’t change it to something better. Thanks!

  • 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-11T19:05:52+00:00Added an answer on May 11, 2026 at 7:05 pm

    If I understand your question correctly you need to use the Elements function:

                var sons_qry = from son in parents.Elements()
                          select son;
    
                var grandsons_qry = from grandson in sons_qry.Descendants()
                                    select grandson;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.