XMLWriter 上的格式设置总是在同一行上写入所有内容



我有一个子,我试图用它来输出一些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

您正在创建设置,但随后不会将它们传递给任何内容:

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的使用。

相关内容

  • 没有找到相关文章

最新更新