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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:14:26+00:00 2026-06-09T15:14:26+00:00

I have an IEnumerable<Item> where Item has a property named widgets which contains xml

  • 0

I have an IEnumerable<Item> where Item has a property named “widgets” which contains xml as a string. The xml contains elements for each widget and each widget can contain an optional element named ‘foo’. For example:

<widgets>
    <widget code="A">
        <foo>1</foo>
    </widget>
    <widget code="B" />
</widgets>

Can I flatten the IEnumerable<Item> into an IEnumerable of an anonymous type which represents a widget?

  • 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-09T15:14:27+00:00Added an answer on June 9, 2026 at 3:14 pm

    I’m not sure if foo can be repeating element or not, but in this case it’s returned as an Enumerable<String>, which is simple enough to change.

    The setup (here there are two items, with the same Widgets XML):

    public class Item
    {
        public string Name;
        public string Widgets;
    }
    
    var xml = "<widgets>\r\n    <widget code=\"A\">\r\n        <foo>1</foo>\r\n    </widget>\r\n    <widge" +
    "t code=\"B\" />\r\n</widgets>";
    
    var item1 = new Item {Name = "I1", Widgets = xml};
    var item2 = new Item {Name = "I2", Widgets = xml};
    var items = new Item[] {item1, item2}.AsEnumerable();
    

    The widget selection process:

    var widgets = 
        from item in items
        from widget in XElement.Parse(item.Widgets).Elements("widget")
        select new {
            Name = item.Name,
            Code = widget.Attribute("code").Value,
            Foo = widget.Elements("foo").Select(f => f.Value)
        };
    

    That gives you 4 items (I1 A, I1 B, I2 A, I2 B) with Name (from Item) and Code and Foo (from the XML) set correctly.

    If you want foo to be a single element, the change is as follows (setting FirstOrDefault basically), which will give you nulls for when foo isn’t around:

    var widgets = 
        from item in items
        from widget in XElement.Parse(item.Widgets).Elements("widget")
        select new {
            Name = item.Name,
            Code = widget.Attribute("code").Value,
            Foo = widget.Elements("foo").Select(f => f.Value).FirstOrDefault()
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object which has a property that contains an IEnumerable of objects.
I have an IEnumerable<KeyValuePair<string,string>> , from which I would like, ideally, an anonymous object
I have an IEnumerable<Person> object. The Person class has a Designation property. I want
I have a IEnumerable. I have a custom Interval class which just has two
I have an ItemsControl that contains items that each has its own DataTemplate .
i have a collection of items where each item has a date field (the
i have a function: private IEnumerable<Promotion> MatchesKeyword(IEnumerable<Promotion> list, String keyword) { ...snip... } which
I have a rest service which has a list of Groups each group has
I have a need to move an item in an IEnumerable<> up, that is
I have an IEnumerable< T> which is declared on the page like this: IEnumerable<

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.