解析FTP XML目录添加到集合

时间:2015-01-02 22:59:07

标签: .net xml vb.net

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

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

1 个答案:

答案 0 :(得分:0)

如何使用LINQ to 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