解析 FTP XML 目录添加到 collection

Parse FTP XML Directory add to collection

我检索了 XML 格式的远程 FTP 结构。我正在尝试解析 XML 并将每个项目添加到 collection 中,例如

TestDir/kernel.dll
TestDir/New folder/
TestDir/New folder/mycert.avi
TestDir/New folder/asdasd
TestDir/New folder/asdasd/asdasd
TestDir/New folder/asdasd/asdasd/asdasdasdasd
TestDir/New folder/asdasd/asdasd/asdasdasdasd/done
TestDir/I7Folder/
TestDir/I7Folder/sdfsdfg.txt
TestDir/I7Folder/sdfsdfsdf.txt

这是我的 XML 数据,

<?xml version="1.0" encoding="utf-8" ?>
<dirTree>
    <dir name="TestDir">
        <file sz="364" dt="Tue, 30 Dec 2014 22:56:00 -0500">kernel.dll</file>
        <dir name="New folder">
            <file sz="309" dt="Tue, 30 Dec 2014 23:00:00 -0500">mycert.avi</file>
                <dir name="asdasd">
                    <dir name="asdasd" />
                    <dir name="asdasdasdasd">
                        <dir name="done" />
                    </dir>
                </dir>
            </dir>
        </dir>
        <dir name="I7Folder">
            <file sz="289" dt="Tue, 30 Dec 2014 22:57:00 -0500">sdfsdfg.txt</file>
            <file sz="280" dt="Tue, 30 Dec 2014 22:57:00 -0500">sdfsdfsdf.txt</file>
        </dir>
        <dir name="data">
            <file sz="4727" dt="Tue, 30 Dec 2014 22:56:00 -0500">dasdasd.txt</file>
            <file sz="78419" dt="Tue, 30 Dec 2014 22:56:00 -0500">asdasdas.txt</file>
            <dir name="New folder">
                <dir name="New folder" />
                <dir name="New folder (2)">
                    <dir name="New folder" />
                </dir>
            </dir>
            <dir name="images" />
        </dir>
    </dir>
</dirTree>

我下面的代码产生了不正确的输出,它会产生类似的东西,

TestDir/New folder/New folder/asdasd/asdasd/asdasdasdasd/done/I7Folder/sdfsdfsdf.txt

我如何执行循环,以便它不会继续使用不属于正确位置的文件夹名称。我认为这是一个简单的问题,但我似乎无法理解它,因为我已经研究了很长时间了。

Dim doc As New XmlDocument
doc.LoadXml(strXml)

Dim x As XmlNode
For Each x In doc("dirTree").ChildNodes
    Dim strDir As String = ""

    Select Case x.Name
        Case "file"
            colCurrentFiles.Add(x.InnerText)
        Case "dir"
            strDir = x.Attributes(0).Value
            colCurrentFolders.Add(strDir)
            LoopDirectory(x, "", colCurrentFiles, colCurrentFolders, strDir)
            Exit Sub
    End Select
Next
Private Sub LoopDirectory(x As XmlNode, strDir As String, colCurrentFiles As Collection, colCurrentFolders As Collection, strParentDirectory As String)
    Debug.Write(strDir & vbNewLine)
    Dim y As XmlNode

    For Each y In x.ChildNodes
        Select Case y.Name
            Case "file"
                colCurrentFiles.Add(strDir & "\" & y.InnerText)
            Case "dir"
                If strDir = "" Then
                    strDir = strParentDirectory & "\" & y.Attributes(0).Value
                Else
                    strDir = strDir & "\" & y.Attributes(0).Value
                End If

                Debug.Write(strDir & vbNewLine)
                colCurrentFolders.Add(strDir)

                LoopDirectory(y, strDir, colCurrentFiles, colCurrentFolders, strParentDirectory)
        End Select
    Next
End Sub

如何使用 LINQ 来 XML 而不是旧的 XmlDocument API?

Sub Main()
    Dim doc As XDocument = XDocument.Load("Input.xml")

    Dim items As List(Of String) = LoopDirectory(doc.Root, "").ToList()

    For Each item In items
        Console.WriteLine(item)
    Next
End Sub

Private Function LoopDirectory(dir As XElement, path As String) As IEnumerable(Of String)
    Dim collection As New List(Of String)

    For Each element In dir.Elements
        If element.Name.LocalName = "file" Then
            collection.Add(String.Format("{0}\{1}", path, CStr(element)))
        Else
            Dim newPath = String.Format("{0}\{1}", path, CStr(element.Attribute("name")))
            collection.Add(newPath)
            collection.AddRange(LoopDirectory(element, newPath))
        End If
    Next

    Return collection
End Function

版画

\TestDir
\TestDir\kernel.dll
\TestDir\New folder
\TestDir\New folder\mycert.avi
\TestDir\New folder\asdasd
\TestDir\New folder\asdasd\asdasd
\TestDir\New folder\asdasd\asdasdasdasd
\TestDir\New folder\asdasd\asdasdasdasd\done
\TestDir\I7Folder
\TestDir\I7Folder\sdfsdfg.txt
\TestDir\I7Folder\sdfsdfsdf.txt
\TestDir\data
\TestDir\data\dasdasd.txt
\TestDir\data\asdasdas.txt
\TestDir\data\New folder
\TestDir\data\New folder\New folder
\TestDir\data\New folder\New folder (2)
\TestDir\data\New folder\New folder (2)\New folder
\TestDir\data\images