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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:53:11+00:00 2026-05-12T18:53:11+00:00

I’m having trouble groking something in Linq – maybe someone can give me some

  • 0

I’m having trouble groking something in Linq – maybe someone can give me some tips.

I have XML that I need to transform to new XML. The problem with this is that it is I need several iterations of transformation to get it right.

The source would look something like this:

    <Meals>
      <ketchup/>
      <steak/>
      <mustard/>
      <thigh/>
      <fillet/>
      <penne/>
      <drumstick/>
      <steak/>
      <ketchup/>
      <fillet/>
      <fillet/>
      <macaroni/>
      <drumstick/>
      <thigh/>
      <ketchup/>
      <thigh/>
      <fillet/>
    </Meals>

What I’d like this to end up being is:

    <Meals>
      <Meal>
        <ketchup/>
        <steak/>
        <mustard/>
        <thigh/>
      </Meal>
      <Meal>
        <fillet/>
        <penne/>
        <drumstick/>
      </Meal >
      <Meal>
        <steak/>
        <ketchup/>
      </Meal>
      <Meal>
        <fillet/>
      </Meal>
      <Meal>
        <fillet/>
        <macaroni/>
        <drumstick/>
      </Meal>
      <Meal>
        <thigh/>
        <ketchup/>
      </Meal>
      <Meal>
        <thigh/>
        <fillet/>
      </Meal>
    </Meals>

The logic here is that:

  • <steak> and <fillet> are beef
    meats – anything that occurs before
    or after them that is not a beef meat
    part of the meal so long as it’s not already part of another meal. If beef meat occurs after a beef meat, a new meal is created at the element of the new beef meat.
  • <thigh> and <drumstick> are
    chicken meats – anything that occurs
    before or after them that is not a
    chicken meat is part of the meal so long as it’s not already part of another meal. If chicken meat occurs after a chicken meat, a new meal is created at the element of the new chicken meat.
  • <macaroni> and <penne> are pastas anything that occurs before or after them that is not a pasta is
    part of the meal so long as it’s not already part of another meal. If pasta occurs after a pasta, a new meal is created at the element of the new pasta.
  • <ketchup> and <mustard> are
    condiments – they can be part of any
    meal.

I’ve started with ElementsAfterSelf and ElementsBeforeSelf and TakeWhile, but am running into walls with my understanding of how to perform the above. I’m using VB.NET and Linq, but can read C#.

Any thoughts or pointers?

  • 1 1 Answer
  • 1 View
  • 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-12T18:53:11+00:00Added an answer on May 12, 2026 at 6:53 pm

    Got a good answer from another forum and here’s the final:

        Dim meals As XElement = <Meals>
                                    <ketchup/>
                                    <steak/>
                                    <mustard/>
                                    <thigh/>
                                    <fillet/>
                                    <macaroni/>
                                    <drumstick/>
                                    <thigh/>
                                    <ketchup/>
                                    <thigh/>
                                    <fillet/>
                                </Meals>
        Dim newMeals As XElement = <Meals/>
        Dim meal As XElement = <Meal/>
        Dim hasBeef As Boolean
        Dim hasChicken As Boolean
        For Each m In meals.Descendants()
            Select Case m.Name
                Case Is = "steak", "fillet"
                    If hasBeef Then
                        newMeals.Add(meal) 
                        meal = <Meal/> 
                        hasChicken = False
                    Else
                        hasBeef = True
                    End If
                    meal.Add(m)
                Case Is = "drumstick", "thigh"
                    If hasChicken Then
                        newMeals.Add(meal)
                        meal = <Meal/>
                        hasBeef = False
                    Else
                        hasChicken = True
                    End If
                    meal.Add(m)
                Case Else
                    meal.Add(m)
            End Select
        Next
        newMeals.Add(meal)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.