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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:48:35+00:00 2026-05-27T00:48:35+00:00

I have generated an XmlDocument and populated it with the recursive traversal of a

  • 0

I have generated an XmlDocument and populated it with the recursive traversal of a file path. The document looks more or less like this:

<?xml version="1.0" encoding="utf-16"?>
<Tree>
  <Directory name="Root" path="C:\Root">
    <Directory name="1" path="C:\Root\1">
      <Directory name="3" path="C:\Root\1\3">
        <File name="5.png" size="198525440" path="C:\Root\1\3\5.png" />
        <File name="6.png" size="736621" path="C:\Root\1\3\6.png" />
      </Directory>
      <File name="2.png" size="761944" path="C:\Root\1\2.png" />
      <File name="3.png" size="2663" path="C:\Root\1\3.png" />
    </Directory>
    <Directory name="2" path="C:\Root\2">
      <File name="4.png" size="324187" path="C:\Root\2\4.png" />
    </Directory>
    <File name="1.png" size="2663" path="C:\Root\1.png" />
  </Directory>
</Tree>

For the directory structure:

C:
C:\Root
C:\1
C:\1\3
C:\1\3\5.png
C:\1\3\6.png
C:\1\2.png
C:\1\3.png
C:\2
C:\2\4.png
C:\1.png

Given the above tree, the goal is to write the tree to the console, in Human readable form as follows:

Root/
    +-- 1/
    |    +-- 3/
    |    |    +-- 5.png
    |    |    +-- 6.png
    |    +-- 2.png
    |    +-- 3.png
    |    +-- 2/
    |    |    +-- 4.png
    +-- 1.png

Now, I can quite easily write a function that recurses the structure, outputting the required text, but I’m thinking that XSLT could potentially transform the XmlDocument into the form shown above in a somewhat simpler way. The only question I would ask is… how? I’m a bit of a noob when it comes to XSLT (meaning, a total noob). I’m thinking use of a transform would be a more flexible method to hard coding a traverse and output function.

Further to this, it would be nice to be able to sort the XML tree by either name or file size, whilst retaining the file tree structure (sort children of each node). Can this be done with XSLT, or should I code my own IComparer to do this?

Thanks for any assistance you can give me.

  • 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-27T00:48:36+00:00Added an answer on May 27, 2026 at 12:48 am

    Sorting the document can be easily done with XSLT, here is an XSLT 1.0 stylesheet that sorts any Directory child elements by the name attribute and any File child elements by an attribute name set with a parameter:

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0">
    
      <xsl:param name="sort-key" select="'size'"/>
      <xsl:param name="sort-type" select="'number'"/>
      <xsl:param name="sort-order" select="'ascending'"/>
    
      <xsl:output indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="Directory">
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates select="Directory">
            <xsl:sort select="@name"
                      data-type="text"
                      order="{$sort-order}"/>
          </xsl:apply-templates>
          <xsl:apply-templates select="File">
            <xsl:sort select="@*[local-name() = $sort-key]"
                      data-type="{$sort-type}"
                      order="{$sort-order}"/>
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a XML file that looks like this <Licence> <Name>Test company</Name> <Version>1.1.1.1</Version> <NumberOfServer>2</NumberOfServer>
I have an XML document (generated by Adobe XFA forms), that contains data like
I have generated an XML file that and one of the nodes contains data
I am getting this error when parsing an incorrectly-generated XML document: org.xml.sax.SAXParseException: The value
I have an XML document generated from an external application, but that application does
I have generated a set of classes using xsd.exe and created an XML document
I'm having trouble deserializing my XML file from a MemoryStream. I have a generated
I have an XML document generated by a 3rd party using BizTalk, and I
I have a dynamically generated XML document made with PHP DOMDocument . I use
I'm new to LINQ and C# but I have an xml file generated from

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.