我有一个sub我试图用来获取一些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
答案 0 :(得分: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
的使用。)