Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSXML creating XML "header"

I am attempting to use MSXML in VB6 to create a XML file that can then be deserialized as an object in C#.

The XML I am attempting to mimic looks like this

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfStock xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Stock>
    <ProductCode>12345</ProductCode>
    <ProductPrice>10.32</ProductPrice>
  </Stock>
  <Stock>
    <ProductCode>45632</ProductCode>
    <ProductPrice>5.43</ProductPrice>
  </Stock>
</ArrayOfStock>

The question I have is how do I create the following line using the MSXML library?

<?xml version="1.0" encoding="utf-8"?>

IE: How do I create an unterminated "header" value?

like image 367
Maxim Gershkovich Avatar asked Nov 24 '25 12:11

Maxim Gershkovich


2 Answers

Have a look at this similar question.

You need to use a MXXMLWriter60, instead of saving it directly. ... See IMXWriter for details.

like image 118
Josh M. Avatar answered Nov 26 '25 02:11

Josh M.


Lots of hubbub here about UTF-8, but the DOMDocument.save() method does use the PI to determine how to encode saved output. The only real snag is that for formatted output instead of economical output (no whitespace) you need to use the SAX Writer.

Basically things seem to work just as expected though. There is nothing hackish about this, it's how it is done.

Option Explicit

Private Sub Main()
    Dim varStock As Variant
    Dim docStock As MSXML2.DOMDocument
    Dim elemRoot As MSXML2.IXMLDOMElement
    Dim elemStock As MSXML2.IXMLDOMElement
    Dim elemField As MSXML2.IXMLDOMElement
    Dim I As Integer
    
    varStock = Array(Array("12345", 10.32), _
                     Array("¥45632", 5.43)) 'Yen sign used here to show Unicode.
    
    Set docStock = New MSXML2.DOMDocument
    With docStock
        .appendChild .createProcessingInstruction("xml", _
                                                  "version=""1.0"" encoding=""utf-8""")
        Set elemRoot = .createElement("ArrayOfStock")
        With elemRoot
            .setAttribute "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"
            .setAttribute "xmlns:xsd", "http://www.w3.org/2001/XMLSchema"
            For I = 0 To UBound(varStock)
                Set elemStock = docStock.createElement("Stock")
                With elemStock
                    Set elemField = docStock.createElement("ProductCode")
                    elemField.Text = CStr(varStock(I)(0))
                    .appendChild elemField
                    Set elemField = docStock.createElement("ProductPrice")
                    elemField.Text = CStr(varStock(I)(1))
                    .appendChild elemField
                End With
                .appendChild elemStock
            Next
        End With
        Set .documentElement = elemRoot
        On Error Resume Next
        Kill "created.xml"
        On Error GoTo 0
        .save "created.xml"
    End With
End Sub

Examining the output file looking for the Yen sign you should see that the text is UTF-8 encoded.

If you want this in-memory rather than to disk you can .save() to something like an ADODB.Stream object, or just use XMLHTTPRequest.send with the DOMDocument as the argument (body). There is no need to resort to the heavyweight option of an Interop approach.

like image 23
Bob77 Avatar answered Nov 26 '25 03:11

Bob77



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!