Question

I have a sub that im trying to use to get some XML out put, but no matter what I do the output file writes everything on the same line : Here is my code below, can you advice me on where I might be goign wrong ?

 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
Was it helpful?

Solution

You're creating the settings, but then not passing them to anything:

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

You should use them when you create the XmlWriter:

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

(I've separated out the filename from the writer creation just for formatting, but please note the use of Path.Combine as well.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top