Can I add a linefeed or other formatting between MSXML2.IXMLDOMAttribute nodes?

141 views Asked by At

I am trying to add white space when I export data from an Excel VBA DOMDocument to XML.

I can successfully do these between normal node and elements of any type, but I'd like to add a linefeed and some white space for indentation between two attributes of an element.

Example: Get this...

<elements>
<element id="idvalue">
    <subelement id="idvalue" item-type="10" store-category="3" purchase-limit="1" min-roster-space="4" ui-headerbg="tex.icon" ui-icon="tex.coolicon">

To look like this...

<elements>
<element id="idvalue">
    <subelement id="idvalue" item-type="10" store-category="3" 
    purchase-limit="1" min-roster-space="4" ui-headerbg="tex.icon" 
    ui-icon="tex.coolicon">

Doing this with normal elements is fairly straightforward using code similar to this:

    parentElement.InsertBefore _
        parentElement.OwnerDocument.createTextNode(strElementFormat), _
        parentElement.ChildNodes.Item(0)

or

    parentElement.appendChild _
        parentElement.OwnerDocument.createTextNode(strLastElementFormat)

I've tried iterating through the attributes as nodes, inserting before using the parent element, etc.

It seems to treat the second text element as part of the attribute value, like so:

    <item id="2024_MINNOW_CONVERSION" item-type="10" store-category="3" purchase-limit="1&#xA;            " min-roster-space="4">

Using this code snippet:

Sub formatXMLAttributes(currentElement As MSXML2.IXMLDOMElement, _
                    toolXMLDoc As MSXML2.DOMDocument60, _
                    Indent As Integer)
    Dim attributeNode As MSXML2.IXMLDOMNode, _
        intIndex As Integer, _
        strElementFormat As String

    intIndex = 0
    strElementFormat = vbCrLf & Space$(Indent * 4)
    For Each attributeNode In currentElement.Attributes
        intIndex = intIndex + 1
        If intIndex Mod 4 = 0 And currentElement.Attributes.Length > 4 Then
           attributeNode.appendChild _
           attributeNode.OwnerDocument.createTextNode(strElementFormat)
        End If
    Next
End Sub

Any thoughts? My suspicion is that it is not supported

0

There are 0 answers