0

我有一个子程序,我试图用它来输出一些 XML,但是无论我做什么,输出文件都会将所有内容都写在同一行:这是我的代码,你能告诉我我可能出错的地方吗?

 Private Sub CreateActionFile()

        Dim lobjDirectory As New DirectoryInfo(mstrFolderName)
        Dim lobjFiles As New List(Of FileInfo)
        lobjFiles.AddRange(lobjDirectory.GetFiles("*.xml"))
        Dim lobjWriter As XmlWriter = XmlWriter.Create(mstrFolderName & "\" & txtActionName.Text & ".xml")

        Dim lobjSettings As New XmlWriterSettings()

        lobjSettings.Indent = True
        lobjSettings.NewLineOnAttributes = True

        Dim lstrStartFileNumber As Integer = CInt(txtFrom.Text)
        Dim lstrEndFileNumber As Integer = CInt(txtTo.Text)

        lobjWriter.WriteStartElement("Action")
        lobjWriter.WriteAttributeString("ID", Me.txtID.Text)
        lobjWriter.WriteAttributeString("Name", Me.txtActionName.Text)
        lobjWriter.WriteAttributeString("Type", "XML")
        lobjWriter.WriteStartElement("xml")
        While lstrEndFileNumber > lstrStartFileNumber
            For Each lobjFile As FileInfo In lobjFiles
                If lobjFile.Name.StartsWith(lstrStartFileNumber.ToString) Then
                    Dim lobjXmlDom As New XmlDocument
                    lobjXmlDom.Load(lobjFile.FullName)
                    lobjXmlDom.WriteContentTo(lobjWriter)
                    lstrStartFileNumber = lstrStartFileNumber + 1
                Else
                    'Nothing - move on
                End If
            Next
        End While
        lobjWriter.WriteEndElement()
        lobjWriter.WriteEndElement()
        lobjWriter.WriteEndDocument()

        lobjWriter.Close()

    End Sub
4

1 回答 1

3

您正在创建设置,但没有将它们传递给任何东西:

Dim lobjSettings As New XmlWriterSettings()
lobjSettings.Indent = True
lobjSettings.NewLineOnAttributes = True

您应该在创建时使用它们XmlWriter

Dim filename = Path.Combine(mstrFolderName, txtActionName.Text & ".xml")
Dim lobjWriter = XmlWriter.Create(filename, lobjSettings)

(我已经将文件名从编写器创建中分离出来只是为了格式化,但请注意使用Path.Combine。)

于 2012-11-19T16:30:16.353 回答