Click here to Skip to main content
15,672,129 members
Articles / Programming Languages / C#
Posted 17 Sep 2003


31 bookmarked

XML Directory Tree Generator

Rate me:
Please Sign up or sign in to vote.
3.33/5 (10 votes)
17 Sep 2003CPOL1 min read
Describes a directory tree documenter that outputs XML.

Image 1


This code snippet recursively enumerates all the folders and files under a given starting folder and generates an XML document as an output, that represents the same hierarchy as of the traversed directories. The resulting XML document can then be transformed with a simple XSLT transformation to generate the desired output. The main reason for writing this was to display the contents of a given directory on a website, without having to enable directory browsing on the server. I wanted to be able to define certain filter criteria to control the output generated, so it was important to capture as much information pertaining to the folders and files as possible.

Using the code

The code is implemented as FileSystemInfoLister class. The main entry point is the GetFileSystemInfoList() method that returns an XML document. The AddElements() method is called recursively until the entire path is traversed.

using System;
using System.Xml;
using System.IO;

namespace Hosca.FileSystemInfoLister {

    public class FileSystemInfoLister {

        XmlDocument xmlDoc;

        public FileSystemInfoLister() {
            xmlDoc = new XmlDocument();

        public XmlDocument GetFileSystemInfoList(string StartFolder) {

                XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", 
                                                                "", "yes");
                xmlDoc.PrependChild ( xmlDec );
                XmlElement nodeElem =  XmlElement("folder",
                         new DirectoryInfo(StartFolder).Name); 
            catch(Exception ex) {
                return xmlDoc;
            return xmlDoc;

        private XmlElement AddElements(XmlElement startNode, 
                                               string Folder){
                DirectoryInfo dir = new DirectoryInfo(Folder);
                DirectoryInfo[] subDirs = dir.GetDirectories() ;
                FileInfo[] files = dir.GetFiles();
                foreach(FileInfo fi in files){
                  XmlElement fileElem = XmlElement("file",fi.Name);
                   ((fi.Attributes & FileAttributes.Hidden) != 0) 
                                                       ? "Y":"N"));
                   ((fi.Attributes & FileAttributes.Archive ) != 0) 
                                                       ? "Y":"N"));
                   ((fi.Attributes & FileAttributes.System ) != 0) 
                                                       ? "Y":"N"));
                   ((fi.Attributes & FileAttributes.ReadOnly ) != 0) 
                                                        ? "Y":"N"));
                foreach (DirectoryInfo sd in subDirs) {
                  XmlElement folderElem = XmlElement("folder",sd.Name);
                    ((sd.Attributes & FileAttributes.Hidden) != 0) 
                                                       ? "Y":"N"));
                    ((sd.Attributes & FileAttributes.System  ) != 0) 
                                                       ? "Y":"N"));
                    ((sd.Attributes & FileAttributes.ReadOnly ) != 0)
                                                       ? "Y":"N"));
                return startNode;
            catch(Exception ex) {
                return XmlElement("error",ex.Message);
        private XmlAttribute XmlAttribute(string attributeName, 
                                          string attributeValue){
            XmlAttribute xmlAttrib = 
            xmlAttrib.Value = FilterXMLString(attributeValue);
            return xmlAttrib;
        private XmlElement XmlElement(string elementName, 
                                       string elementValue){
            XmlElement xmlElement = xmlDoc.CreateElement(elementName);
            return xmlElement;
        private string FilterXMLString(string inputString){
            string returnString = inputString;
            if (inputString.IndexOf("&") > 0){
                returnString = inputString.Replace("&","&");
            if (inputString.IndexOf("'") > 0){
                returnString = inputString.Replace("'","'");
            return returnString;

Points of interest

The only minor point of interest is that there are 2 characters that are allowable in file names but not in XML documents. In fact there are 5 characters that are not allowed in XML documents. They are :

<<less than
>>greater than
""quotation mark

Fortunately for us, Windows takes care of 3 of them. However the ampersand and the apostrophe which are legal to use in file names will have to be handled manually. The FilterXMLString method takes care of the details.


  • Sept 18 2003 - Initial version (1.0.0)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Web Developer JPMorgan Chase & Co.
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralMy vote of 1 Pin
movhlps29-Nov-08 5:20
movhlps29-Nov-08 5:20 
QuestionEditing XML file Pin
Krishnakumar Bhagwat21-Aug-06 21:17
Krishnakumar Bhagwat21-Aug-06 21:17 
Answerapp Pin
Member 45204226-Dec-09 1:27
Member 45204226-Dec-09 1:27 
GeneralSmall bug in FilterXMLString Pin
Richard Poole12-May-04 6:15
Richard Poole12-May-04 6:15 
GeneralRe: Small bug in FilterXMLString Pin
Erhan Hosca12-May-04 7:15
professionalErhan Hosca12-May-04 7:15 
GeneralRe: Small bug in FilterXMLString Pin
djarn29-Oct-04 22:34
djarn29-Oct-04 22:34 
GeneralHandling Ampersands Pin
Donald Xie11-Jan-04 15:11
Donald Xie11-Jan-04 15:11 
GeneralRe: Handling Ampersands Pin
Erhan Hosca12-Jan-04 3:29
professionalErhan Hosca12-Jan-04 3:29 
GeneralVery cool - I want more! Pin
Shem Stimpy25-Sep-03 3:32
Shem Stimpy25-Sep-03 3:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.