2013-04-12 162 views
0

我有一个文件和子目录与文件的目录,并希望从它们创建xml。这里是我的文件夹结构:从目录结构创建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. 有没有一种方法,我可以用我的代码与名称属性排序。

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

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; 

    } 

enter image description here

回答

1

您可以添加一个将要处理的子目录

方法
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; 
     } 
+0

感谢消除了第一个文件夹名称...我现在应该如何排序它应该放在字典<字符串,字符串>还是有更好的方法吗? – James

+0

检查编辑! –

4

快到了:只是一些小的修改自己的代码是你所需要的。

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; 

    } 
+0

+1好答案。我重新格式化了你的代码。 –

+0

感谢您的答案..我现在唯一的问题是它列出“inputdata”文件夹。我怎么会去按名称排序呢? – James

+0

@JimMischel谢谢! –

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;