2013-04-12 212 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;