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

  • SEARCH
  • Home
  • 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 178807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:17:44+00:00 2026-05-11T14:17:44+00:00

Grettings! I have some XML that looks like this: <Root> <SectionA> <Item id=111> <Options>

  • 0

Grettings!

I have some XML that looks like this:

<Root>     <SectionA>         <Item id='111'>             <Options>                 <Option val='a' cat='zzz'>                     <Package value='apple' />                     <Feature value='avacado' />                 </Option>                 <Option val='b' cat='yyy'>                     <Package value='banana' />                     <Feature value='blueberry' />                 </Option>             </Options>         </Item>         <Item id='222'>             <Options>                 <Option val='c' cat='xxx'>                     <Package value='carrot' />                     <Feature value='cucumber' />                 </Option>                 <Option val='d' cat='www'>                     <Package value='dairy' />                     <Feature value='durom' />                 </Option>             </Options>         </Item>     </SectionA>     <SectionB>     .     .     .     </SectionB> </Root> 

I’d like to get the PACKAGE and FEATURE values based on the ID attribute of ITEM being ‘111’ and the VAL attribute of OPTION being ‘a’.

I’m not sure where to start. I’m able to select the ITEM node using a where, but I’m not sure how to combine that with a where clause on the OPTION node. Any ideas?

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

    This works for me.

    var doc = XDocument.Parse(s);  var items = from item in doc.Descendants('Item')             where item.Attribute('id').Value == '111'             from option in item.Descendants('Option')             where option.Attribute('val').Value == 'a'             let package = option.Element('Package').Attribute('value')             let feature = option.Element('Feature').Attribute('value')             select new { Package = package.Value, Feature = feature.Value };  items.First().Feature; // = 'avacado' items.First().Package; // = 'apple' 

    You can omit the let parts if you want, they are only to make the anonymous type thinner.

    var items = from item in doc.Descendants('Item')             where item.Attribute('id').Value == '111'             from option in item.Descendants('Option')             where option.Attribute('val').Value == 'a'             select new             {                Package = option.Element('Package').Attribute('value').Value,                Feature = option.Element('Feature').Attribute('value').Value             }; 

    Actually, I kind of like the second one more.


    And the non query Linq style.

    var items = doc.Descendants('Item')                .Where(item => item.Attribute('id').Value == '111')                .SelectMany(item => item.Descendants('Option'))                .Where(option => option.Attribute('val').Value == 'a')                .Select(option => new                {                 Package = option.Element('Package').Attribute('value').Value,                 Feature = option.Element('Feature').Attribute('value').Value                }); 
    • 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.