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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:39:31+00:00 2026-05-27T20:39:31+00:00

I got the following XML file (Data.xml): <root> <sitecollection name=1A> <site name=1B> <maingroup name=1C>

  • 0

I got the following XML file (Data.xml):

<root>
 <sitecollection name="1A">
   <site name="1B">
     <maingroup name="1C"> 
        <group name="1D"> </group>
     </maingroup> 
    </site>
 </sitecollection>
 <sitecollection name="2A">
   <site name="2B">
     <maingroup name="2C"> 
        <group name="2D"> </group>
     </maingroup> 
    </site>
 </sitecollection>
</root>

And I need to print all the all the child elements in this format:

1A
 1B
  1C
   1D
2A
 2B
  2C
   2D

I have the following code so far which needs some adjustment. I could also change it completely if there’s an easier method. Thanks for your help

class xmlreader
{
    public static void Main()
    {

        // Xdocument to read XML file
        XDocument xdoc = XDocument.Load("Data.xml");
        var result = new System.Text.StringBuilder();

        var lv1s = from lv1 in xdoc.Descendants("sitecollection")
                   select new
                   {
                       sitecollection = lv1.Attribute("name").Value,
                       maingroup = lv1.Descendants("group")

                   };
        var lv2s = from lv2 in xdoc.Descendants("site")
                   select new
                   {
                       site = lv2.Attribute("name").Value,
                       sitetittle = lv2.Descendants()
                   };
        var lv3s = from lv3 in xdoc.Descendants("maingroup")
                   select new
                   {
                       maingroup = lv3.Attribute("name").Value,
                   };
        var lv4s = from lv4 in xdoc.Descendants("group")
                   select new
                   {
                       grouppage = lv4.Attribute("name").Value,
                   };


        // Loop to print results
        foreach (var lv1 in lv1s)
        {
            result.AppendLine(lv1.sitecollection);
            foreach (var lv2 in lv2s)
            {
                result.AppendLine("   " + lv2.Attribute("name").Value);

                foreach (var lv3 in lv3s)
                {
                    result.AppendLine("   " + lv3.Attribute("name").Value);

                    foreach (var lv4 in lv4s)
                    {
                        result.AppendLine("   " + lv4.Attribute("name").Value);
                    }
                }
            }
        }
    }
}
  • 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-27T20:39:31+00:00Added an answer on May 27, 2026 at 8:39 pm

    With such a uniform hierarchy, recursion can do the job with a lot less code:

        void PrintNames(StringBuilder result, string indent, XElement el)
        {
            var attr = el.Attributes("name");
            if (attr != null)
            {
                result.Append(indent);
                result.Append(attr.Value);
                result.Append(System.Environment.NewLine);
            }
            indent = indent + " ";
            foreach(var child in el.Elements())
            {
                PrintNames(result, indent, child);
            }
        }
    
    ...
    
    var sb = new StringBuilder();
    PrintNames(sb, String.Empty, xdoc.Root);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've got following example xml: <entity id=1> <name>computer</name> <type>category</type> <entities> <entity id=2> <name>mac</name> <type>category</type>
I have an XML file in the following structure. <NewDataSet> <markers> <name>name text</name> <desc>desc
Let´s say I´ve got the following xml string: <Item Name=\$Resources:myresource,somestring;\</Item> Now I want to
At the moment i've got the following code to get data from an xml
I have this XML file from which I'm grabbing all the data: <?xml version=1.0
I've got the following sql code: declare @x xml set @x = '<ResultBlock> <MatchSummary
I have GPS data stored as as .tcx file. This is a xml file
I'm trying to write xml data with XmlLite on buffer but couldn't got any
I've been racking my head with this... I've got a localized strings.xml file in
I have following code to serialize my data into a file: out = new

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.