-1

我有一个代表目录树的 XML。该文件如下所示:

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

但有很多节点

这是要下载的原始 XML。http://depositfiles.org/files/dicjwa5s1

我需要为每个路径生成一个列表,如下所示: Fantasy folder/Glas heim/one folfer/../file Fantasy folder/GlasHeim/another folder/another file 我在网上搜索了类似的问题,但我只找到了这个一:XML 文件中标记路径的 C# 列表 我尝试通过这种方式制作它,但仍然有问题。

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"));
    }
}

这是一个很好的方法,但并不完美,因为它在不同类型节点(文件夹和文件)的文件夹中存在问题

谢谢

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>    
4

1 回答 1

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;
    }
}
于 2013-09-21T21:21:29.687 回答