2

I'm totally new to XML; I just started to study something for my culture using VB6 (! I know... !), without a real goal at the moment. In my first attempt I got a surprising good result, but there is a minor problem which only impact on the visual output. The first line of the XML file is a standard preamble: ?xml version="1.0" encoding="iso-8859-1"?. The second line is the node DB info and it should follow the preamble on a new line, in this way:

<?xml version="1.0" encoding="iso-8859-1"?>
<DB>Created on 15/06/2021</DB>

But instead this second line continues on the first. I tried to insert a text node with a newline (as seen on this site), but I get an error, which translated, say something as: "'impossible to execute the operation with a node of type PCDATA". My code is:

Sub MakeDB()
Dim dom As MSXML2.DOMDocument60
Dim root As IXMLDOMElement
Dim node As MSXML2.IXMLDOMNode

   Set dom = New MSXML2.DOMDocument60
   dom.async = False
   dom.preserveWhiteSpace = True
   
   Set node = dom.createProcessingInstruction("xml", "version='1.0' encoding='iso-8859-1'" + vbNewLine)
   dom.appendChild node
   Set node = Nothing
'the following lines raise the error 
   Set node = dom.createTextNode(vbNewLine)
   dom.appendChild node
   Set node = Nothing

' Create the first element.
    Set root = dom.createElement("DB")   
    Set node = dom.createTextNode("Created on " + CStr(Date))
    root.appendChild node
    Set node = Nothing
    dom.appendChild root
    ...

I realize that this is not a real problem, but I would like to understand what happens.

StayOnTarget
  • 9,925
  • 10
  • 45
  • 68
Orionis
  • 851
  • 7
  • 11

1 Answers1

1

Don't be concerned with that newline between the XML declaration and the document element when creating the content.

You can control whether or not to have newlines or output as one big line with serialization option of the Writer:

https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms764689(v=vs.85)

Sets whether to indent output. When set to True, this property indents output for "pretty printing" to reflect the nesting structure of the document.

Boolean. Read/write. The default is False. The general rules of indenting elements are: XML headers and root elements start on a new line with a zero indent; element content, including leading and trailing white spaces, is not changed in any way.

oMXXMLWriter.indent = True 
Mads Hansen
  • 59,712
  • 12
  • 110
  • 139
  • I'm using [this SO solution](https://stackoverflow.com/questions/6405236/forcing-msxml-to-format-xml-output-with-indents-and-newlines) for years. – Hel O'Ween Jun 16 '21 at 13:31
  • I'm not concerned, but the two lines joined togher are really not acceptable, I seen a lot of XML documents well and totally formatted. I wonder why I can not find a way to keep them separated. The declaration node is destroyed in code so why VB6 still consider it still accessible? The Writer you refer is the MSXML2.MXHTMLWriter60? I discovered it from your answer and have to understande what is and what it does. – Orionis Jun 16 '21 at 16:09