VB 6.0 XML Output File Not Containing line feed character

Posted on 2003-11-24
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
LVL 14

Accepted Solution

waty earned 500 total points
ID: 9830972
' #Mandix Repository#*****************************************************
' * Programmer Name  : J R
' * Web Site         :
' * E-Mail           :
' * 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:
' 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel VBA combine two working workbooks 8 49
VBA open file from excel cell 4 36
MsgBox 2 49
Excel - Save a copy of work book 13 83
Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now