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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:05:47+00:00 2026-05-28T20:05:47+00:00

I have an XML that is laid out to be reformatted into nested HTML

  • 0

I have an XML that is laid out to be reformatted into nested HTML table headers. I am working on getting each tier of the XML document into it’s own list. For example:

<column name="Total" size="0">
  <column name="Users" size="0" />
</column>
<column name="Date" size="0" />
<column name="Unique" size="0">
  <column name="Clicks" size="0">
    <column name="RC" size="0" />
    <column name="CB" size="0" />
  </column>
</column>

From this example, columns “Total”, “Date”, and “Unique” should be in the first list. Columns “Users” and “Clicks” should be in the second list. And, columns “RC” and “CB” should be in the third list. This should be accomplished using recursion to make the method completely dynamic. Any help is greatly appreciated.

  • 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-28T20:05:48+00:00Added an answer on May 28, 2026 at 8:05 pm

    Here you go:

    XElement root = XElement.Parse(@"
        <doc>
          <column1>
            <column2 />
          </column1>
          <column3 />
          <column4>
            <column5>
              <column6 />
              <column7 />
            </column5>
          </column4>
        </doc>");
    
    List<List<XElement>> outerList = new List<List<XElement>>();
    List<XElement> innerList = root.Elements().ToList();
    while (innerList.Any())
    {
        outerList.Add(innerList);
        innerList = innerList.SelectMany(element => element.Elements()).ToList();
    }
    

    Edit: If you want to strip ancestor XElement instances of their descendants within your list, then you could use the following:

    XElement root = XElement.Parse(@"
        <table>
          <column name=""Total"" size=""0"">
            <column name=""Users"" size=""0"" />
          </column>
          <column name=""Date"" size=""0"" />
          <column name=""Unique"" size=""0"">
            <column name=""Clicks"" size=""0"">
              <column name=""RC"" size=""0"" />
              <column name=""CB"" size=""0"" />
            </column>
          </column>
        </table>");
    
    List<List<XElement>> outerList = new List<List<XElement>>();
    IEnumerable<XElement> innerList = root.Elements();
    while (innerList.Any())
    {
        outerList.Add(innerList.Select(e => new XElement(e.Name, e.Attributes())).ToList());
        innerList = innerList.SelectMany(element => element.Elements());
    }
    

    Note: For the record, your intuition that you should use recursion was correct. However, it is also well known that any recursive function can be converted to an iteration, typically by simulating the stack. Sometimes, this leads to bloated code; however, other times, the conversion lends itself naturally. In your case, if you were to recurse, your recursive parameter would have been the immediate children of the set of elements currently being considered – which already happens to be available in innerList, thus allowing us to use the innerList = innerList.<SequenceOperation> trick to substitute the recursion.

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

Sidebar

Related Questions

I have XML tag that has the content which is HTML document. <xml-tag> <!--
I have XML files that are representation of a portion of HTML code. Those
I have an xml that has nested tags like the following: <?xml version=1.0 encoding=utf-8
I have a list of items that I want to be laid out horizontally.
I have xml that I read it to a table cell but it seems
I have XML that looks something like this: <Root xmlns=http://widgetspecA.com/ns> ...any... <WidgetBox> <A/> <B/>
I have XML that looks like this: <Parameter Name=parameter name Value=parameter value /> which
I have XML that looks like this: <ROW ref=0005631 type=04 line=1 value=Australia/> <ROW ref=0005631
So I have xml that looks like this: <todo-list> <id type=integer>#{id}</id> <name>#{name}</name> <description>#{description}</description> <project-id
My question is the following. I have xml that is versioned by a namespace.

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.