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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:53:24+00:00 2026-06-02T10:53:24+00:00

I am trying to group a flat XML data into hierarchial folders. The code

  • 0

I am trying to group a flat XML data into hierarchial folders.

The code shown below works. I am trying to find the following:

  1. How can I change the synatx for Console.WriteLine("\t\t{0},
    f.Element("FILE_NAME").Value); to something like f.FileName in
    the inner foreach statement.
  2. Is there anything else I can do to make it more efficient and/or readable?

I am currently on 4.0, but wouldn’t mind learning about new features of newer versions that make this beeter

Thanks

Code:

using System;
using System.Linq;
using System.Xml.Linq;

namespace TestingStuff
{
    class LinqQuestion
    {
        static void Main(string[] args)
        {
            Question();

            #region End Console
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("*".PadRight(30, '*'));
            Console.WriteLine("Done");

#if DEBUG
            {
                Console.WriteLine("Press any key to exit.");
                Console.WriteLine("Where is the any key?  --Homer Simpson");
                Console.ReadKey(true);
            }
#endif
            #endregion
        }

        private static void Question()
        {
            int count = 0;
            XDocument xmlDoc = XDocument.Parse(
                @"<ROWSET>
                    <ROW>
                      <PARENT_DIR>Parent_100</PARENT_DIR>
                      <DIR>Folder_110</DIR>
                      <FILE_NAME>File_111</FILE_NAME>
                    </ROW>
                    <ROW>
                      <PARENT_DIR>Parent_100</PARENT_DIR>
                      <DIR>Folder_110</DIR>
                      <FILE_NAME>File_112</FILE_NAME>
                    </ROW>
                    <ROW>
                      <PARENT_DIR>Parent_200</PARENT_DIR>
                      <DIR>Folder_210</DIR>
                      <FILE_NAME>File_211</FILE_NAME>
                    </ROW>
                    <ROW>
                      <PARENT_DIR>Parent_200</PARENT_DIR>
                      <DIR>Folder_220</DIR>
                      <FILE_NAME>File_221</FILE_NAME>
                    </ROW>
                  </ROWSET>");


            var rows = from d in xmlDoc.Descendants("ROW")
                       group d by new
                       {   
                           ParentDir = d.Element("PARENT_DIR").Value,
                           Dir = d.Element("DIR").Value
                       }
                       ;
            foreach (var myRow in rows)
            {

                /* 
                 * Create Folders
                 * Folder: PARENT_DIR\DIR\FILE_NAME
                 * 
                 */
                try
                {

                    string[] folders = new string[] { myRow.Key.ParentDir, myRow.Key.Dir };
                    string newFolder = String.Join("\\", folders);

                    count++;

                    Console.WriteLine("{0}\t{1}", count, newFolder);

                    foreach (var f in myRow)
                    {
                        Console.WriteLine("\t\t{0}", f.Element("FILE_NAME").Value);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error in Copy Process:");
                    // Specify the XML details for failed file / row
                    Console.WriteLine(ex.Message);
                }

            }
            Console.WriteLine();
            Console.WriteLine("Total Count: {0}", count);
            Console.WriteLine();
            Console.WriteLine();
        }
    }
}
  • 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-02T10:53:25+00:00Added an answer on June 2, 2026 at 10:53 am

    Based on a different post in SO, I managed to get what I needed.

    Modifed Linq code: (only the main parts are written)

        var expr = from d in xmlDoc.Descendants("ROW")
                            group d by new
                            {
                               ParentDir = d.Element("PARENT_DIR").Value,
                               Dir = d.Element("DIR").Value,
    
                            } into gDir
                            select new
                            {
                               GroupKey = gDir.Key,
                               Data = from z in gDir
                                      select new { FileName = z.Element("FILE_NAME").Value }
    
                            };
    
    foreach (var g in expr)
    {
       Console.WriteLine("{0}\t\t", g.GroupKey.ParentDir, g.GroupKey.Dir);
    
       foreach (var n in g.Data)
                   Console.WriteLine("\t\t{0}", n.FileName);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to group the following string into three groups. 0:0:Awesome:awesome That being "
I'm trying to understand how LINQ can be used to group data by intervals
I'm trying to group data from the following three tables to create a new
I am trying to group sibling data in an XML file. Given : <?xml
*I am trying to group set of data according some condition using FORTRAN code.
I am trying to group my data based on age. I use the following
I'm trying to group a set of data based on the range of an
I am trying to group a data collection and send it to the View
Trying to work with groupby so that I can group together files that were
I am trying to group row data from a 3-dimensional array by a column

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.