MSXML creates XML header "

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

The XML I'm trying to simulate is as follows

<?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>

I have a question, how to create the next line using the MSXML library?

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

IE: How to create an immutable "header"?

+3
source share
4 answers

Look at this similar question .

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

+3
source

hubbub UTF-8, DOMDocument.save() PI , . , ( ) SAX Writer.

, . , .

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

, , , UTF-8.

, , .save() - ADODB.Stream XMLHTTPRequest.send DOMDocument (). Interop.

+2

"".

XML- omitXMLDeclaration False encoding "utf-8".

+1

Thank you for your input, but, unfortunately, the methods described apply only to xml on the .NET platform.

(But you led me in the right direction)

In VB6 (using MSXML 3 and above), the method of doing what I was looking for was createProcessingInstruction()

The code is as follows.

Private Sub BuildHeader()
    m_document.appendChild m_document.createProcessingInstruction("xml", "version=""1.0"" encoding=""utf-8""")
End Sub

and then can be processed as such (provided that all other details of the object are agreed)

XmlSerializer serializer = new XmlSerializer(typeof(Stock));
using (StreamReader streamReader = new StreamReader(path))
{
    return (Stock)serializer.Deserialize(streamReader);
}
+1
source

All Articles