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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:21:53+00:00 2026-06-17T12:21:53+00:00

I have written: public string GetOutline(int indentLevel, XElement element) { StringBuilder result = new

  • 0

I have written:

        public string GetOutline(int indentLevel, XElement element)
        {
        StringBuilder result = new StringBuilder();


        result = result.AppendLine(new string('-', indentLevel * 2) + element.Name);

        foreach (var childElement in element.Elements())
        {
            result.Append(GetOutline(indentLevel + 3, childElement));
        }

        return result.ToString();
    }

The result of this recursive methond run on xml file shows:

    Videos
    ------Video
    ------------Title
    ------------Director
    ------------Actors
    ------------------Actor
    ------------------Actor
    ------------------Actor
    ------------------Actor
    ------------Length
    ------------Format
    ------------Rating
    ------Video
    ------------Title
    ------------Director
    ------------Length
    ------------Format
    ------------Rating
enter code here

But I would want the output to be like below:

    Videos
    ------Video
    ------------Title
    ------------Director
    ------------Actors
    ------------------Actor
    ------------Length
    ------------Format
    ------------Rating

How to edit the code ? Really struggling with this from yesterday …

  • 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-17T12:21:54+00:00Added an answer on June 17, 2026 at 12:21 pm

    Use linq grouping by name:

    foreach (var childElement in element.Elements().GroupBy(childElement => childElement.Name))
    {
        result.Append(GetOutline(indentLevel + 3, childElement.First()));
    }
    

    instead of:

    foreach (var childElement in element.Elements())
    {
        result.Append(GetOutline(indentLevel + 3, childElement.First()));
    }
    

    The only problem of this method is the use of First.
    If you have, for example in your XML file:

    <actors>
        <actor>
            <property/>
        </actor>
        <actor>
            <differentProperty/>
            <differentProperty2/>
        </actor>
    </actors>
    

    With grouping and First the result will be:

    actors
    ----actor
    --------property
    

    How do you want the result to appear in this case?

    actors
    ----actor
    --------property
    --------differentProperty
    --------differentProperty2
    

    or

    actors
    ----actor
    --------property
    ----actor
    --------differentProperty
    --------differentProperty2
    

    or this case will not appear?

    UPDATE:

    Given your comment the solution would be:

    public static string GetOutline(int indentLevel, XElement element)
    {
        return GetGroupOutline(indentLevel, new[] {element});
    }
    
    /// <summary>
    /// Returns the outline of the elements that constitute a group.
    /// </summary>
    /// <param name="indentLevel">Indent level.</param>
    /// <param name="elements">Elements of a group (all the element of the collection must have the same <c>Name</c>)</param>
    /// <returns>Outline of the group.</returns>
    private static string GetGroupOutline(int indentLevel, IEnumerable<XElement> elements)
    {
        StringBuilder result = new StringBuilder();
    
        // Adds the group name in the outline
        result = result.AppendLine(new string('-', indentLevel * 6) + elements.First().Name);
    
        foreach (var childGroup in from element in elements  // Gets each element in the group "elements"
                                   from childElement in element.Elements()  // Gets their children
                                   group childElement by childElement.Name into childGroup  // Groups the children of all elements by their name to a new group called "childGroup"
                                   select childGroup)
            result.Append(GetGroupOutline(indentLevel + 1, childGroup));  // Shows the outline of the group called "groupChild"
    
        return result.ToString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written a method in java like this public boolean ADD(String ID,String Name,String
I have wriiten a method like this public ArrayList<T> GetDoctorDetail(String name) { if (name!=null
Hi I have written code like this @Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() {
I have written a small webservice[Axis2/Java] which exposes a method public String Fill(String cacheName
i have written the next code: public void delete(MyType instance) { List<MyType> myList =
I have written a custom server control which (pseudo-code) looks like public class MyCustomCtrl
I have written a sample application to write to a public and private queues
In our application, I have seen code written like this: User.java (User entity) public
I have a simple recursive method written in both Java and C# Java public
In physics library written in C# I have the following code: (in ContactManager.cs) public

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.