1

我有一个包含文件的目录和一个包含文件的子目录,并希望从中创建 xml。这是我的文件夹结构:

C:\inputdata folder contains:
C:\inputdata\file1.txt
C:\inputdata\picture1.jpg
C:\inputdata\subfolder\picture2.jpg
C:\inputdata\subfolder\file2.txt
C:\inputdata\subfolder\anotherfolder \file3.txt
C:\inputdata\anotherfolder\

我想生成这个 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<serverfiles>
     <file name="picture1.jpg"/>
     <file name="file1.txt"/>
     <folder name="subfolder">
          <file name="picture2.jpg"/>
          <file name="file2.txt"/>
          <folder name="anotherfolder">
               <file name="file3.txt"/>
          </folder>
     </folder>
     <folder name="anotherfolder">
     </folder>
</serverfiles>

我已经编写了以下控制台应用程序,但我有两个问题。

  1. 这会产生附加的屏幕截图 xml,它在结构上与上面的 xml 不完全相同。

  2. 有没有一种方法可以使用我的代码使用 name 属性对其进行排序。

有人可以请我指出如何做到这一点的正确方向:

    private const string folderLocation = @"c:\inputdata";
    static void Main(string[] args)
    {
        DirectoryInfo dir = new DirectoryInfo(folderLocation);


        var doc = new XDocument(CREATEXML(dir));

        Console.WriteLine(doc.ToString());

        Console.Read();
    }

  private static XElement CREATEXML(DirectoryInfo dir)
    {
        //get directories
        var xmlInfo = new XElement("serverfiles", new XAttribute("name", dir.Name));

        //get all the files first
        foreach(var file in dir.GetFiles())
        {
            xmlInfo.Add(new XElement("file", new XAttribute("name", file.Name)));
        }

        //get subdirectories
        foreach(var subDir in dir.GetDirectories())
        {
            xmlInfo.Add(CREATEXML(subDir));
        }

        return xmlInfo;

    }

在此处输入图像描述

4

3 回答 3

5

几乎就在那里:只需对代码进行一些小的编辑即可。

    private const string folderLocation = @"c:\inputdata";
    static void Main(string[] args)
    {
        DirectoryInfo dir = new DirectoryInfo(folderLocation);

        // makes everything wrapped in an XElement called serverfiles.
        // Also a declaration as specified (sorry about the standalone status:
        // it's required in the XDeclaration constructor)    
        var doc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
             CREATEXML(dir));

        Console.WriteLine(doc.ToString());

        Console.Read();
    }

  private static XElement CREATEXML(DirectoryInfo dir, bool writingServerFiles = true)
    {
        //get directories
        var xmlInfo = new XElement(writingServerFiles ? "serverfiles" : "folder", writingServerFiles ? null : new XAttribute("name", dir.Name)); //fixes your small isue (making the root serverfiles and the rest folder, and serverfiles not having a name XAttribute)

        //get all the files first
        foreach(var file in dir.GetFiles())
        {
            xmlInfo.Add(new XElement("file", new XAttribute("name", file.Name)));
        }
            //get subdirectories
        foreach(var subDir in dir.GetDirectories())
        {
            xmlInfo.Add(CREATEXML(subDir), false);
        }
        return xmlInfo;

    }
于 2013-04-12T21:43:50.127 回答
2

您可以添加另一种处理子目录的方法

private static XElement CreateXML(DirectoryInfo dir)
    {
        var xmlInfo = new XElement("serverfiles");
        //get all the files first
        foreach (var file in dir.GetFiles())
        {
            xmlInfo.Add(new XElement("file", new XAttribute("name", file.Name)));
        }
        //get subdirectories
        foreach (var subDir in dir.GetDirectories())
        {
            xmlInfo.Add(CreateSubdirectoryXML(subDir));
        }
        return xmlInfo;
    }

    private static XElement CreateSubdirectoryXML(DirectoryInfo dir)
    {
        //get directories
        var xmlInfo = new XElement("folder", new XAttribute("name", dir.Name));
        //get all the files first
        foreach (var file in dir.GetFiles())
        {
            xmlInfo.Add(new XElement("file", new XAttribute("name", file.Name)));
        }
        //get subdirectories
        foreach (var subDir in dir.GetDirectories())
        {
            xmlInfo.Add(CreateSubdirectoryXML(subDir));
        }
        return xmlInfo;
    }

编辑:

添加排序:

private static XElement CreateXML(DirectoryInfo dir)
        {
            var xmlInfo = new XElement("serverfiles");
            //get all the files first
            foreach (var file in dir.GetFiles())
            {
                xmlInfo.Add(new XElement("file", new XAttribute("name", file.Name)));
            }
            //get subdirectories
            var subdirectories = dir.GetDirectories().ToList().OrderBy(d => d.Name);
            foreach (var subDir in subdirectories)
            {
                xmlInfo.Add(CreateSubdirectoryXML(subDir));
            }
            return xmlInfo;
        }

        private static XElement CreateSubdirectoryXML(DirectoryInfo dir)
        {
            //get directories
            var xmlInfo = new XElement("folder", new XAttribute("name", dir.Name));
            //get all the files first
            foreach (var file in dir.GetFiles())
            {
                xmlInfo.Add(new XElement("file", new XAttribute("name", file.Name)));
            }
            //get subdirectories
            var subdirectories = dir.GetDirectories().ToList().OrderBy(d => d.Name);
            foreach (var subDir in subdirectories)
            {
                xmlInfo.Add(CreateSubdirectoryXML(subDir));
            }
            return xmlInfo;
        }
于 2013-04-12T21:51:04.213 回答
0

我认为这个解决方案可以更好

        //get directories
        var xmlInfo = new XElement("folder",                 
            new XElement("name", dir.Name),
            new XElement("lastModify", dir.LastWriteTime),
            new XElement("Attributes", dir.Attributes));

        //get subdirectories
        foreach (var subDir in dir.GetDirectories())
        {
            xmlInfo.Add(CREATEXML(subDir));
        }

        //get all the files
        foreach (var file in dir.GetFiles())
        {
            xmlInfo.Add(new XElement("File",
                new XElement("name", file.Name),
                new XElement("size", file.Length),
                new XElement("lastModify", file.LastWriteTime),
                new XElement("Attributes", file.Attributes.ToString())));
        }
        return xmlInfo;
于 2013-08-28T10:20:47.043 回答