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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:23:14+00:00 2026-05-29T23:23:14+00:00

I am trying to parse some XML that looks similar to this: <document> <headings>

  • 0

I am trying to parse some XML that looks similar to this:

<document>
    <headings>
        Important heading stuff.
    </headings>
    <startGroup group="1" />
        <startItem value="1" />Item one stuff<endItem />
        <blockofdata>
            <startItem value="2" />Item two stuff<endItem />
            <startItem value="3" />Item three stuff<endItem />
        </blockofdata>
        <startItem value="4" />Item four stuff<endItem />
    <endGroup />
    <startGroup group="2" />
        <startItem value="1" />Item one stuff<endItem />
        <startItem value="2" />Item two stuff<endItem />
        <startItem value="3" />Item three stuff<endItem />
    <endGroup />
</document>

I cannot figure out a linq-to-xml statement to get what I want. I need to flatten the structure. So assuming the above XML, I would like to get a list of this POCO:

class Items
{
    public int GroupNumber {get;set;} // group property of startGroup
    public int ItemNumber {get;set;} // value property of startItem
    public string ItemText {get;set;}  // data between i
}

How do you write a linq-to-xml statement that would pull the data between the attributes into the above item while grabing the data from between startGroup/endGroup and the data between startItem/endItem? I have burned up several hours on this and am about to just switch to using a XML stream reader and parsing it the old fashioned way.

  • 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-29T23:23:17+00:00Added an answer on May 29, 2026 at 11:23 pm

    The key here is to use the ElementsAfterSelf() and NodesAfterSelf() methods to grab the sibling nodes along with the TakeWhile() predicate to stop enumerating at the appropriate times.

    First the helper methods:

    public static Items ItemsFromStartItem(XElement start, XElement group)
    {
        return new Items
        {
            GroupNumber = (int)group.Attribute("group"),
            ItemNumber = (int)start.Attribute("value"),
            ItemText = start.NodesAfterSelf()
                .TakeWhile(n => n.NodeType != XmlNodeType.Element
                             || ((XElement)n).Name != "endItem")
                .OfType<XText>()
                .Select(t => t.Value)
                .Single()
        };
    }
    
    public static IEnumerable<Items> ItemsFromBlockOfData(
        XElement block, XElement group)
    {
        return block.Elements("startItem")
            .Select(start => ItemsFromStartItem(start, group));
    }
    

    And the magic query.

    var query = doc.Descendants("startGroup")
        .SelectMany(group => group.ElementsAfterSelf()
            .TakeWhile(e => e.Name != "endGroup")
            .SelectMany(e => e.Name == "startItem"
                ? new[] { ItemsFromStartItem(e, group) }
                : ItemsFromBlockOfData(e, group))
        );
    

    Now I hope you did not design this XML yourself… this is the kind of stuff that can really push someone over the edge. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use BeautifulSoup to parse some XML that looks like <a> <b>
I'm trying to parse some poorly generated xml code with scala that looks like
Trying to parse some XML but apparently this is too much for a lazy
I am trying to parse some XML, however, I get the error above this
I've been trying to parse some huge XML files that LXML won't grok, so
I am trying to parse one XML file that contains some unicode characters.I tried
I'm trying to parse some xml files with lua and I'm stuck on this
I am trying to use XDocument.Parse(string s) to parse some XML that is being
I am currently trying to parse some xml data into an NSDictionary . This
I am trying to parse this xml file, which has some nested nodes. I

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.