VB 6.0 XML Output File Not Containing line feed character

Posted on 2003-11-24
Medium Priority
Last Modified: 2008-09-26
I have written an application that opens an existing XML file. My software is written in VB 6.0 I am able to read the xml file, perform my needed tasks and then write my changes to the xml file. The error that I keep getting is when I append new children to the xml file I don't think I am getting any line feed characters to the xml. When I open the changed xml file with notepad I get changes that look like this

Existing XML file....
More Existing XML file...

What I want is output that look like this

I am using MSXML2.DOMDocument40 and I am attaching my nodes with appendChild
Question by:sld9t
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 14

Accepted Solution

waty earned 1500 total points
ID: 9830972
' #Mandix Repository#*****************************************************
' * Programmer Name  : J R
' * Web Site         : http://www.vbdiamond.com
' * E-Mail           : vb_questions@swbell.net
' * Date             : 06/07/2001
' **********************************************************************
' * Comments         : Format XML it with indented nodes
' *
' * This simple shunk of code uses MSXML 3.0 to take any well-formed
' * XML file specified on the command line and format it with indented
' * nodes. Always creates paired tags, even for empty nodes, with
' * the exception of text and CDATA nodes.
' *
' **********************************************************************

' MSXML3 node type constants used in code
Const NODE_TEXT = 3
' XML indentation formatting program
' Send any questions about this code to:
' vb_questions@swbell.net
' and I'll try to get back to you as quick as I can
' Make sure we don't use anything we didn't declare
Option Explicit
' MSXML3 node type constants used in code
Const NODE_TEXT = 3
Public Sub Main()
   Dim bLoaded          As Boolean
   Dim strFileName      As String
   Dim strDTDFileName   As String
   Dim strStandalone    As String
   Dim xmlDoc           As New DOMDocument
   Dim nodeRoot         As IXMLDOMNode
   Dim strRootNodeName  As String
   Dim fsoFileSys       As New FileSystemObject
   Dim tstrXMLOut       As TextStream
   Dim strXMLOut        As String
   'this is not a robust way to get the command line,
   'support for quotation marks should be added if you care,
   'something else to add is an optional second parameter for the DTD filename
   strFileName = Trim(Command)
   'attempt to load the file into the XML DOM document
   bLoaded = xmlDoc.Load(strFileName)
   If bLoaded Then
      Set tstrXMLOut = fsoFileSys.OpenTextFile(strFileName, ForWriting, True)
      Set nodeRoot = xmlDoc.documentElement
      strRootNodeName = nodeRoot.nodeName
      'get the formatted XML string
      strXMLOut = FormatNodeAsString(nodeRoot)
      Set nodeRoot = Nothing
      Set xmlDoc = Nothing
      strStandalone = "no"
      'code assumes here that there is a DTD file of the same base name as the actual XML file
      strDTDFileName = Left(strFileName, Len(strFileName) - 4) & ".dtd"
      'build the necessary lines for any validXML file
      tstrXMLOut.WriteLine "<!-- <?xml version=" & """" & "1.0" & """" & " standalone=" & """" & strStandalone & """" & "?> -->"
      tstrXMLOut.WriteLine "<!-- <!DOCTYPE " & strRootNodeName & " SYSTEM " & """" & strDTDFileName & """" & "> -->"
      'write out the newly formatted XML string
      tstrXMLOut.Write strXMLOut
      'more cleanup
      Set tstrXMLOut = Nothing
      Set fsoFileSys = Nothing
      'oh goody, we're done
      MsgBox "Success. " & strFileName & " has been formatted.", , "Format Complete"
      MsgBox "Couldn't load the XML file: " & strFileName & vbCrLf & "I quit!", , "File Error"
   End If
End Sub

' This function recurses through the XMLtree given in the nodeIn parameter
' generally this would be the root node,at least that's how I call it but
' it would work to format any subtree aswell
Private Function FormatNodeAsString(nodeIn As IXMLDOMNode, Optional intIndent As Integer = 0) As String
   Dim I                As Integer
   Dim strXML           As String
   Dim bChildIsTextOrCDATA As Boolean
   Dim intDent          As Integer
   intDent = intIndent
   Dim nodemapAttributes As IXMLDOMNamedNodeMap
   Dim nodelistChildren As IXMLDOMNodeList
   Dim nodeTempChild    As IXMLDOMNode
   Dim lngNodeType      As Long
   lngNodeType = nodeIn.nodeType
   Dim strNodeVal       As String
   strNodeVal = nodeIn.nodeTypedValue
   Dim bHasChildren     As Boolean
   If lngNodeType = NODE_CDATA_SECTION Then
      strXML = strXML & "<![CDATA["
   ElseIf lngNodeType = NODE_TEXT Then
      'do nothing
      For I = 1 To intDent
         strXML = strXML & vbTab
      Next I
      strXML = strXML & "<" & nodeIn.nodeName
   End If
   Set nodemapAttributes = nodeIn.Attributes
   If Not (nodemapAttributes Is Nothing) Then
      For I = 0 To nodemapAttributes.length - 1
         Set nodeTempChild = nodemapAttributes.Item(I)
         strXML = strXML & " " & nodeTempChild.nodeName & "=" & """" & nodeTempChild.nodeTypedValue & """"
      Next I
   End If
   Set nodemapAttributes = Nothing
   If (lngNodeType <> NODE_CDATA_SECTION) And (lngNodeType <> NODE_TEXT) Then
      strXML = strXML & ">"
   End If
   Set nodelistChildren = nodeIn.childNodes
   bHasChildren = False
   If nodelistChildren.length > 0 Then
      bHasChildren = True
      Set nodeTempChild = nodelistChildren.Item(0)
      If Not ((nodeTempChild.nodeType = NODE_CDATA_SECTION) Or (nodeTempChild.nodeType = NODE_TEXT)) Then
         strXML = strXML & vbCrLf
         bChildIsTextOrCDATA = False
         bChildIsTextOrCDATA = True
      End If
      Do Until nodeTempChild Is Nothing
         strXML = strXML & FormatNodeAsString(nodeTempChild, intDent + 1)
         Set nodeTempChild = nodeTempChild.nextSibling
      strXML = strXML & strNodeVal
   End If
   Set nodelistChildren = Nothing
   Set nodeTempChild = Nothing
   If lngNodeType = NODE_CDATA_SECTION Then
      FormatNodeAsString = strXML & "]]>"
   ElseIf lngNodeType = NODE_TEXT Then
      FormatNodeAsString = strXML
      If bHasChildren And (Not bChildIsTextOrCDATA) Then
         For I = 1 To intDent
            strXML = strXML & vbTab
         Next I
      End If
      FormatNodeAsString = strXML & "</" & nodeIn.nodeName & ">" & vbCrLf
   End If
End Function

Author Comment

ID: 9842559
Thanks for the ideas J R. Looking over your comments, you are suggesting reprocessing the final xml and restructuring it as an external file. I would like to avoid this if possible I see it being inefficient. I hope that MS has some method built into their xml controls that would allow you to save the xml file with indentation and line feed characters.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question