65.9K
CodeProject 正在变化。 阅读更多。
Home

XML 目录树生成器

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.33/5 (10投票s)

2003年9月18日

CPOL

1分钟阅读

viewsIcon

97371

downloadIcon

1210

描述了一个目录树文档生成器,它输出 XML。

引言

这段代码片段递归地枚举给定起始文件夹下的所有文件夹和文件,并生成一个 XML 文档作为输出,该文档表示与遍历的目录相同的层次结构。生成的 XML 文档然后可以使用简单的 XSLT 转换生成所需的输出。编写此代码的主要原因是想在网站上显示给定目录的内容,而无需在服务器上启用目录浏览。我希望能够定义某些筛选标准来控制生成的输出,因此尽可能捕获与文件夹和文件相关的信息非常重要。

使用代码

该代码实现为 FileSystemInfoLister 类。主要入口点是 GetFileSystemInfoList() 方法,该方法返回一个 XML 文档。AddElements() 方法被递归调用,直到遍历完整个路径。

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) {

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


        private XmlElement AddElements(XmlElement startNode, 
                                               string Folder){
            try{
                DirectoryInfo dir = new DirectoryInfo(Folder);
                DirectoryInfo[] subDirs = dir.GetDirectories() ;
                FileInfo[] files = dir.GetFiles();
                foreach(FileInfo fi in files){
                  XmlElement fileElem = XmlElement("file",fi.Name);
                  fileElem.Attributes.Append(XmlAttribute("Extension", 
                                                           fi.Extension));
                  fileElem.Attributes.Append(XmlAttribute("Hidden", 
                   ((fi.Attributes & FileAttributes.Hidden) != 0) 
                                                       ? "Y":"N"));
                  fileElem.Attributes.Append(XmlAttribute("Archive", 
                   ((fi.Attributes & FileAttributes.Archive ) != 0) 
                                                       ? "Y":"N"));
                  fileElem.Attributes.Append(XmlAttribute("System", 
                   ((fi.Attributes & FileAttributes.System ) != 0) 
                                                       ? "Y":"N"));
                  fileElem.Attributes.Append(XmlAttribute("ReadOnly", 
                   ((fi.Attributes & FileAttributes.ReadOnly ) != 0) 
                                                        ? "Y":"N"));
                  startNode.AppendChild(fileElem);
                }
                foreach (DirectoryInfo sd in subDirs) {
                  XmlElement folderElem = XmlElement("folder",sd.Name);
                  folderElem.Attributes.Append(XmlAttribute("Hidden", 
                    ((sd.Attributes & FileAttributes.Hidden) != 0) 
                                                       ? "Y":"N"));
                  folderElem.Attributes.Append(XmlAttribute("System", 
                    ((sd.Attributes & FileAttributes.System  ) != 0) 
                                                       ? "Y":"N"));
                  folderElem.Attributes.Append(XmlAttribute("ReadOnly",
                    ((sd.Attributes & FileAttributes.ReadOnly ) != 0)
                                                       ? "Y":"N"));
                  startNode.AppendChild(AddElements(folderElem,
                                                     sd.FullName));
                }
                return startNode;
            }
            catch(Exception ex) {
                return XmlElement("error",ex.Message);
            }
        }
        private XmlAttribute XmlAttribute(string attributeName, 
                                          string attributeValue){
            XmlAttribute xmlAttrib = 
                xmlDoc.CreateAttribute(attributeName);
            xmlAttrib.Value = FilterXMLString(attributeValue);
            return xmlAttrib;
        }
        private XmlElement XmlElement(string elementName, 
                                       string elementValue){
            XmlElement xmlElement = xmlDoc.CreateElement(elementName);
            xmlElement.Attributes.Append(XmlAttribute("name", 
                                      FilterXMLString(elementValue)));
            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;
        }    
    }
}

关注点

唯一值得注意的一点是,有两个字符允许在文件名中使用,但不允许在 XML 文档中使用。实际上,有 5 个字符不允许在 XML 文档中使用。它们是

< < less than
> > greater than
& & ampersand
&apos; ' 撇号
" " 引号

幸运的是,Windows 会处理其中 3 个。但是,文件名中允许使用的 & 和撇号必须手动处理。FilterXMLString 方法负责处理细节。

历史

  • 2003年9月18日 - 初始版本 (1.0.0)
© . All rights reserved.