Help and Support

How To Create XML Documents with Binary Data in Visual Basic

Article ID:254388
Last Review:November 23, 2006
Revision:5.1
This article was previously published under Q254388

SUMMARY

XML schema defines the ability to create an XML node as either bin.base64 or bin.hex, which are two forms of encoded binary.

This article shows an example of how to create a XML document with nodes of bin.hex type and read the binary content from this node.

Back to the top

MORE INFORMATION

NOTE: As this example shows, you do not have to do the conversion from binary format to hex format separately. If you assign the binary content to a node of bin.hex via the nodeTypedValue property, the MSXML parser automatically converts it for you. If you indeed want to provide the HEX value, you could assign to the node by using text property.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:
https://partner.microsoft.com/global/30000104 (https://partner.microsoft.com/global/30000104)
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS (http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms)

The following steps show you how to create an XML document with binary content, and then read back the binary content of a node that you create from this XML document:
1.Create a new Visual Basic Standard EXE Project and save it.
2.Select Microsoft XML, Version 2.0 or later, in your project reference.
3.Create a button called cmdCreateXML and another one called cmdGetBinary on form1.
4.Give these buttons descriptive captions.
5.Paste the following code in the code module behind form1.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' How to create XML with binary data
'
' General program flows:
'
' Build  builds a small XML file from a MS Doc file
' Write  saves XML tree to a file
' Write the MS Doc file as another file
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Use the version dependent progid DOMDocument40 to create an MSXML 4.0 DOMDocument object if your are referencing MSXML 4.0 in your project.

Option Explicit
Dim oDoc As DOMDocument
Dim DOCINPATH As String
Dim XMLOUTPATH As String
Dim DOCOUTPATH As String

Private Sub cmdCreateXML_Click()
    
    Dim oEle As IXMLDOMElement
    Dim oRoot As IXMLDOMElement
    Dim oNode As IXMLDOMNode
        
    DOCINPATH = App.Path & "\DocInput.doc"
    XMLOUTPATH = App.Path & "\XmlOuput.xml"
          
    Call ReleaseObjects
    
    Set oDoc = New DOMDocument
    oDoc.resolveExternals = True
    
' Create processing instruction and document root
    Set oNode = oDoc.createProcessingInstruction("xml", "version='1.0'")
    Set oNode = oDoc.insertBefore(oNode, oDoc.childNodes.Item(0))
    
' Create document root
    Set oRoot = oDoc.createElement("Root")
    Set oDoc.documentElement = oRoot
    oRoot.setAttribute "xmlns:dt", "urn:schemas-microsoft-com:datatypes"

' Add a few simple nodes with different datatypes
    Set oNode = oDoc.createElement("Document")
    oNode.Text = "Demo"
    oRoot.appendChild oNode
    
    Set oNode = oDoc.createElement("CreateDate")
    oRoot.appendChild oNode
    Set oEle = oNode
    
' Use DataType so MSXML will validate the data type
    oEle.dataType = "date"
         
    oEle.nodeTypedValue = Now
    
    Set oNode = oDoc.createElement("bgColor")
    oRoot.appendChild oNode
    Set oEle = oNode
    
' Use DataType so MSXML will validate the data type
    oEle.dataType = "bin.hex"
         
    oEle.Text = &HFFCCCC
    
    Set oNode = oDoc.createElement("Data")
    oRoot.appendChild oNode
    Set oEle = oNode
    
' Use DataType so MSXML will validate the data type
    oEle.dataType = "bin.base64"
     
' Read in the data
    oEle.nodeTypedValue = ReadBinData(DOCINPATH)
    
' Save xml file
    oDoc.save XMLOUTPATH
    
    MsgBox XMLOUTPATH & " is created for you."
   
End Sub

Function ReadBinData(ByVal strFileName As String) As Variant
    Dim lLen As Long
    Dim iFile As Integer
    Dim arrBytes() As Byte
    Dim lCount As Long
    Dim strOut As String
    
'Read from disk
    iFile = FreeFile()
    Open strFileName For Binary Access Read As iFile
    lLen = FileLen(strFileName)
    ReDim arrBytes(lLen - 1)
    Get iFile, , arrBytes
    Close iFile
    
    ReadBinData = arrBytes
End Function

Private Sub WriteBinData(ByVal strFileName As String)
    Dim iFile As Integer
    Dim arrBuffer() As Byte
    Dim oNode As IXMLDOMNode
      
    If Not (oDoc Is Nothing) Then
        
' Get the data
        Set oNode = oDoc.documentElement.selectSingleNode("/Root/Data")

' Make sure you use a byte array instead of variant
        arrBuffer = oNode.nodeTypedValue
            
' Write to disk
        
        iFile = FreeFile()
        Open strFileName For Binary Access Write As iFile
        Put iFile, , arrBuffer
        Close iFile
    
    End If
    
End Sub

Private Sub cmdGetBinary_Click()
        
    DOCOUTPATH = App.Path & "\DocOutput.doc"
    
    Set oDoc = New DOMDocument
    
    If oDoc.Load(XMLOUTPATH) = True Then
       ' Save the Doc as another file
       WriteBinData DOCOUTPATH
       
       MsgBox DOCOUTPATH & " is created for you."
    Else
        MsgBox oDoc.parseError.reason
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    ReleaseObjects
End Sub

Private Sub ReleaseObjects()
    Set oDoc = Nothing
End Sub
					
6.Create a Microsoft Word document with some arbitrary content and name it DocInput.doc.
7.Save this Word file in the same folder as your project.
8.Run the project and click the cmdCreateXML button. An XML file named XmlOuput.xml is created.
9.Click the cmdGetBinary button and a Word file called DocOutput.doc is created.

Back to the top

REFERENCES

For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:
http://msdn.microsoft.com/ie/ (http://msdn.microsoft.com/ie/)

http://support.microsoft.com/iep (http://support.microsoft.com/iep)
XML Data Types Reference: http://msdn.microsoft.com/xml/reference/schema/datatypes.asp (http://msdn.microsoft.com/xml/reference/schema/datatypes.asp)

Back to the top


APPLIES TO
Microsoft Visual Basic 6.0 Learning Edition
Microsoft Visual Basic 6.0 Professional Edition
Microsoft Visual Basic 6.0 Enterprise Edition
Microsoft XML Parser 2.5
Microsoft XML Parser 2.6
Microsoft XML Parser 3.0
Microsoft XML Parser 3.0 Service Pack 1
Microsoft XML Core Services 4.0

Back to the top

Keywords: 
kbhowto kbsbnworkshop KB254388

Back to the top

Article Translations

 

Related Support Centers

Other Support Options

  • Need More Help?
    Contact a Support professional by Email, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.