2013-09-21 81 views
-1

我有一個表示目錄樹的XML。該文件是這樣的:C#從XML寫入文件夾

<folder name="FantasyFolders"> 
    <folders> 
     <folder name="GlasHeim"> 
    </folders> 
    <file> 
     <file name=dog> 
    </file> 
</folders> 

,但有很多節點

的下面是原始的XML下載。 http://depositfiles.org/files/dicjwa5s1

我需要生成的每個路徑的列表,像這樣: 幻想文件夾/ GLAS海姆/一個folfer /../文件 幻想文件夾/ GlasHeim /其他文件夾/另一個文件 我搜索了類似的問題在網絡中,但我只找到這一個:C# List of Tag Paths in XML File 我試圖通過這種方式,但仍然有問題。

public void calculatepaths(XmlNode node, string path, string CurrString) 
{ 
    if (!node.HasChildNodes) 
    {enter code here 
     path = path + CurrString; 
     Console.WriteLine(path); 
    } 
    foreach (XmlElement attr in node) 
    { 
    } 
    foreach (XmlNode node2 in node) 
    { 
     path = path + "\\" + ((XmlElement)node).GetAttribute("name"); 
     path = path.Replace("\\\\\\", "\\\\"); 
     calculatepaths(node2, path, ((XmlElement)node2).GetAttribute("name")); 
    } 
} 

這個人是一個很好的形式給出,但不是完美的,因爲它有哪裏diferents樣的節點(文件夾和文件)的

感謝文件夾的問題

XML文件

<?xml version="1.0" encoding="utf-8"?> 
<folder name="FantasyFolders"> 
    <folders> 
     <folder name="Glast Heim"> 
      <folders> 
       <folder name="Bajando por la escalera"> 
        <folders> 
         <folder name="Fondo del corredor"> 
          <folders> 
           <folder name="Bajando la escalera"> 
            <files> 
             <file name="Señor del calabozo.boss" hp="90" damage="36" 
              defense="15" /> 
            </files> 
           </folder> 
           <folder name="Puerta norte"> 
            <files> 
             <file name="Cadaver.txt" hp="50" damage="27" defense="11" /> 
            </files> 
           </folder> 
           <folder name="Puerta sur"> 
            <files> 
             <file name="Cofre rojo.zip" /> 
             <file name="Suspiro.exe" hp="36" damage="35" defense="15" /> 
             <file name="Susurro.dll" hp="35" damage="37" defense="16" /> 
            </files> 
           </folder> 
          </folders> 
          <files> 
           <file name="Rybio.boss" hp="66" damage="29" defense="13" /> 
          </files> 
         </folder> 
         <folder name="Puerta norte"> 
          <folders> 
           <folder name="Hoyo en la pared"> 
            <files> 
             <file name="Cofre oxidado.zip" /> 
            </files> 
           </folder> 
          </folders> 
          <files> 
           <file name="Experimento fallido.mp3" hp="34" damage="33" 
            defense="6" /> 
          </files> 
         </folder> 
         <folder name="Puerta sur"> 
          <files> 
           <file name="Hombre encadenado 1.txt" hp="37" damage="21" 
            defense="7" /> 
           <file name="Hombre encadenado 2.txt" hp="37" damage="17" 
            defense="8" /> 
          </files> 
         </folder> 
        </folders> 
        <files> 
         <file name="Cucaracha Gigante 1.avi" hp="26" damage="19" 
          defense="6" /> 
         <file name="Cucaracha Gigante 2.mov" hp="31" damage="19" 
          defense="5" /> 
        </files> 
       </folder> 
       <folder name="Puerta de la derecha"> 
        <folders> 
         <folder name="Armario"> 
          <folders> 
           <folder name="Cookieverse"> 
            <folders> 
             <folder name="Hacia aca"> 
              <folders> 
               <folder name="Por alla"> 
                <folders> 
                 <folder name="Cookieland"> 
                  <files> 
                   <file name="Cofre del tesoro.zip" /> 
                   <file name="Galleta furiosa.txt" hp="80" damage="37" 
                    defense="20" /> 
                  </files> 
                 </folder> 
                </folders> 
                <files> 
                 <file name="Galleta de soda.txt" hp="70" damage="34" 
                  defense="16" /> 
                </files> 
               </folder> 
               <folder name="Por Aqui"> 
                <folders> 
                 <folder name="Cookiecore"> 
                  <files> 
                   <file name="Grandmatriarch.boss" hp="100" damage="52" 
                    defense="26" /> 
                  </files> 
                 </folder> 
                </folders> 
                <files> 
                 <file name="Galleta magica.txt" hp="70" damage="30" 
                  defense="17" /> 
                </files> 
               </folder> 
              </folders> 
              <files> 
               <file name="Galleta mutante.txt" hp="57" damage="30" 
                defense="14" /> 
              </files> 
             </folder> 
             <folder name="Hacia alla"> 
              <folders> 
               <folder name="Caliente caliente"> 
                <folders> 
                 <folder name="Cookiemore"> 
                  <files> 
                   <file name="Baúl de galletas.zip" /> 
                   <file name="Galleta latiguda.txt" hp="80" damage="32" 
                    defense="20" /> 
                  </files> 
                 </folder> 
                </folders> 
                <files> 
                 <file name="Galleta de chocolate.txt" hp="69" damage="30" 
                  defense="16" /> 
                </files> 
               </folder> 
               <folder name="Frio frio"> 
                <folders> 
                 <folder name="Cookievale"> 
                  <files> 
                   <file name="Arbol de galletas.zip" /> 
                   <file name="Cómeme.txt" hp="76" damage="32" 
                    defense="20" /> 
                  </files> 
                 </folder> 
                </folders> 
                <files> 
                 <file name="Galleta de mantequilla.txt" hp="69" 
                  damage="35" defense="16" /> 
                </files> 
               </folder> 
              </folders> 
              <files> 
               <file name="Galleta poderosa.txt" hp="59" damage="29" 
                defense="13" /> 
              </files> 
             </folder> 
            </folders> 
            <files> 
             <file name="Galleta zombie.txt" hp="47" damage="24" 
              defense="11" /> 
            </files> 
           </folder> 
           <folder name="Narnia"> 
            <folders> 
             <folder name="Castillo de hielo"> 
              <folders> 
               <folder name="Sala del trono"> 
                <folders> 
                 <folder name="Puerta del norte"> 
                  <folders> 
                   <folder name="Sala del tesoro"> 
                    <files> 
                     <file name="Cofre de hielo.zip" /> 
                    </files> 
                   </folder> 
                  </folders> 
                  <files> 
                   <file name="Aslan.avi" hp="76" damage="42" defense="21" /> 
                  </files> 
                 </folder> 
                </folders> 
               </folder> 
              </folders> 
              <files> 
               <file name="Golem.exe" hp="44" damage="37" defense="18" /> 
              </files> 
             </folder> 
            </folders> 
           </folder> 
          </folders> 
         </folder> 
         <folder name="Puerta del fondo"> 
          <folders> 
           <folder name="Cruzando el puente"> 
            <files> 
             <file name="Executioner.exe" hp="35" damage="36" defense="16" /> 
             <file name="Mysteltainn.dll" hp="38" damage="35" defense="14" /> 
             <file name="Tyrfing.dll" hp="36" damage="33" defense="16" /> 
            </files> 
           </folder> 
           <folder name="Subiendo la escalera"> 
            <folders> 
             <folder name="Fondo del corredor"> 
              <folders> 
               <folder name="Puerta naranja"> 
                <folders> 
                 <folder name="Terraza"> 
                  <files> 
                   <file name="Cofre mojado.zip" /> 
                   <file name="Viento del mal.dll" hp="67" damage="42" 
                    defense="24" /> 
                  </files> 
                 </folder> 
                </folders> 
                <files> 
                 <file name="Hombre malo.mid" hp="63" damage="42" 
                  defense="15" /> 
                </files> 
               </folder> 
               <folder name="Puerta verde"> 
                <folders> 
                 <folder name="Puerta este"> 
                  <files> 
                   <file name="Alice.ppt" hp="85" damage="36" defense="20" /> 
                   <file name="Aliza.pptx" hp="87" damage="36" 
                    defense="21" /> 
                   <file name="Alizei.ppt" hp="89" damage="32" 
                    defense="20" /> 
                   <file name="Alizée.pptx" hp="87" damage="36" 
                    defense="21" /> 
                  </files> 
                 </folder> 
                </folders> 
                <files> 
                 <file name="Chimera.avi" hp="67" damage="38" defense="18" /> 
                </files> 
               </folder> 
               <folder name="Subiendo la escalera"> 
                <folders> 
                 <folder name="Puerta este"> 
                  <folders> 
                   <folder name="Puerta adornada"> 
                    <files> 
                     <file name="Payaso maligno.exe" hp="77" damage="50" 
                      defense="26" /> 
                    </files> 
                   </folder> 
                  </folders> 
                  <files> 
                   <file name="Necrófago 1.dll" hp="65" damage="43" 
                    defense="25" /> 
                   <file name="Necrófago 2.dll" hp="66" damage="39" 
                    defense="24" /> 
                  </files> 
                 </folder> 
                 <folder name="Puerta noreste"> 
                  <files> 
                   <file name="Mago rojo.mp3" hp="74" damage="49" 
                    defense="18" /> 
                  </files> 
                 </folder> 
                 <folder name="Puerta noroeste"> 
                  <files> 
                   <file name="Mago azul.mp3" hp="72" damage="49" 
                    defense="19" /> 
                  </files> 
                 </folder> 
                 <folder name="Puerta norte"> 
                  <folders> 
                   <folder name="Puerta simple"> 
                    <files> 
                     <file name="Mago piel.wav" hp="84" damage="50" 
                      defense="22" /> 
                    </files> 
                   </folder> 
                  </folders> 
                  <files> 
                   <file name="Mago negro.mid" hp="73" damage="45" 
                    defense="18" /> 
                  </files> 
                 </folder> 
                 <folder name="Puerta sudeste"> 
                  <files> 
                   <file name="Mago rosadito.mid" hp="72" damage="44" 
                    defense="18" /> 
                  </files> 
                 </folder> 
                 <folder name="Puerta sur"> 
                  <files> 
                   <file name="Mago morado.wav" hp="75" damage="47" 
                    defense="19" /> 
                  </files> 
                 </folder> 
                 <folder name="Puerta suroeste"> 
                  <files> 
                   <file name="Otro mago azul.wav" hp="75" damage="46" 
                    defense="19" /> 
                  </files> 
                 </folder> 
                </folders> 
                <files> 
                 <file name="Caballero del abismo.boss" hp="92" 
                  damage="40" defense="21" /> 
                </files> 
               </folder> 
              </folders> 
              <files> 
               <file name="Arquero medianamente oscuro.xlsx" hp="61" 
                damage="29" defense="14" /> 
               <file name="Arquero no tan oscuro.xlsx" hp="64" damage="31" 
                defense="15" /> 
              </files> 
             </folder> 
             <folder name="Puerta azul"> 
              <files> 
               <file name="Hombre misterioso.xlsx" hp="62" damage="29" 
                defense="14" /> 
              </files> 
             </folder> 
             <folder name="Puerta violeta"> 
              <folders> 
               <folder name="Puerta del fondo"> 
                <files> 
                 <file name="Criatura extraña.mov" hp="71" damage="40" 
                  defense="18" /> 
                </files> 
               </folder> 
              </folders> 
              <files> 
               <file name="Adepto 1.ppt" hp="62" damage="26" defense="15" /> 
               <file name="Adepto 2.ppt" hp="63" damage="28" defense="15" /> 
              </files> 
             </folder> 
            </folders> 
            <files> 
             <file name="Arquero oscurito.xlsx" hp="48" damage="26" 
              defense="10" /> 
             <file name="Arquero oscuro.xls" hp="50" damage="24" 
              defense="11" /> 
            </files> 
           </folder> 
          </folders> 
          <files> 
           <file name="Raydric.boss" hp="80" damage="29" defense="13" /> 
          </files> 
         </folder> 
         <folder name="Trampilla"> 
          <files> 
           <file name="Cofre pequeño.zip" /> 
          </files> 
         </folder> 
        </folders> 
        <files> 
         <file name="Zombie 1.txt" hp="27" damage="17" defense="5" /> 
         <file name="Zombie 2.txt" hp="29" damage="17" defense="4" /> 
        </files> 
       </folder> 
       <folder name="Subiendo la escalera"> 
        <folders> 
         <folder name="Puerta norte"> 
          <files> 
           <file name="Cofre ensangrentado.zip" /> 
           <file name="Fanático.doc" hp="47" damage="17" defense="8" /> 
          </files> 
         </folder> 
         <folder name="Subiendo la escalera"> 
          <folders> 
           <folder name="Puerta este"> 
            <files> 
             <file name="Cofre sucio.zip" /> 
             <file name="Criatura con ojos desorbitados.mov" hp="51" 
              damage="29" defense="11" /> 
            </files> 
           </folder> 
           <folder name="Subiendo la escalera"> 
            <files> 
             <file name="Caballero sangriento.boss" hp="74" damage="34" 
              defense="16" /> 
            </files> 
           </folder> 
          </folders> 
          <files> 
           <file name="Edward el vampiro.xlsx" hp="43" damage="22" 
            defense="7" /> 
           <file name="Fanático.doc" hp="50" damage="13" defense="8" /> 
          </files> 
         </folder> 
        </folders> 
        <files> 
         <file name="Novato.txt" hp="30" damage="13" defense="4" /> 
         <file name="Otro zombie.txt" hp="27" damage="18" defense="5" /> 
        </files> 
       </folder> 
       <folder name="Umbral"> 
        <folders> 
         <folder name="Altar"> 
          <folders> 
           <folder name="Cámara norte"> 
            <files> 
             <file name="Señor Oscuro.boss" hp="82" damage="32" 
              defense="15" /> 
            </files> 
           </folder> 
           <folder name="Cámara sur"> 
            <files> 
             <file name="Mosca Vampiro.ppt" hp="58" damage="20" 
              defense="12" /> 
             <file name="Mosca Zombie 1.mov" hp="52" damage="30" 
              defense="12" /> 
             <file name="Mosca Zombie 2.mov" hp="50" damage="30" 
              defense="11" /> 
            </files> 
           </folder> 
           <folder name="Detras del altar"> 
            <folders> 
             <folder name="Hoyo en la pared"> 
              <files> 
               <file name="Mimic.boss" hp="93" damage="43" defense="18" /> 
              </files> 
             </folder> 
            </folders> 
            <files> 
             <file name="Ilusión 1.exe" hp="34" damage="37" defense="15" /> 
             <file name="Ilusión 2.exe" hp="36" damage="27" defense="15" /> 
             <file name="Sacerdote maligno.ppt" hp="60" damage="22" 
              defense="12" /> 
            </files> 
           </folder> 
          </folders> 
          <files> 
           <file name="Necrófago 1.doc" hp="44" damage="14" defense="8" /> 
           <file name="Necrófago 2.docx" hp="59" damage="19" defense="8" /> 
          </files> 
         </folder> 
        </folders> 
        <files> 
         <file name="Espectro.dll" hp="16" damage="25" defense="8" /> 
        </files> 
       </folder> 
      </folders> 
      <files> 
       <file name="Zombie.txt" hp="20" damage="15" defense="1" /> 
      </files> 
     </folder> 
    </folders> 
    <files> 
     <file name="It's Dangerous in there.zip" /> 
     <file name="Take this.zip" /> 
    </files> 
</folder>  
+0

我不知道你在問什麼。您是否嘗試從XML文件創建文件夾結構?或者你是否試圖將目錄結構是什麼序列化爲XML文件? – Freeqaz

+0

我試圖從XML文件創建一個文件夾結構。 –

+0

訪問被拒絕。無法下載XML文件。你能把文件放在另一臺服務器上嗎? – Tony

回答

0

我試圖從XML文件創建一個文件夾結構。

使用LINQ to XML

var rootDir = @"e:\temp"; 
var folders = XDocument.Load(filename) 
       .Descendants("folder") 
       .Select(x => Path.Combine(GetParents(x).Reverse().ToArray())) 
       .OrderBy(x => x); 

foreach(var dir in folders) 
{ 
    Directory.CreateDirectory(Path.Combine(rootDir,dir)); 
} 

IEnumerable<string> GetParents(XElement x) 
{ 
    while (x != null) 
    { 
     if (x.Name.LocalName == "folder") yield return x.Attribute("name").Value; 
     x = x.Parent; 
    } 
} 
+0

感謝它與文件夾完美配合,但文件沒有出現。有什麼辦法可以創建文件嗎? –

+0

@EnzoZerega你是什麼意思,有沒有什麼辦法來創建文件。你打算寫什麼作爲他們的內容? – I4V

+0

目前,我只需要創建一個非內容文件,例如我可以在任何文件夾中使用FileStream創建文件「random.anyextension」st = new FileStream(path \\ random.anyextension,Filemode.OpenOrCreate)。在XML中,文件夾裏面有文件,每個文件夾都有一個特定的擴展名(doc,zip等),但是它與「功能」或內容無關,我只需要該文件爲「random.anyextension」。 –