perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
home » focus » msxml » ask a question » past questions & answers Tuesday, 14 August 2007
 
NEWS
MSXML 4.0 SP2 now available!

 
MSXML
Basics
DOM
SAX
XPath
XSLT
Schemas
SOM
HTTP Access
.NET
Data Islands
Ask a Question
   Past Q&As
C++ Samples
DLL/Version Info
Reference Guide
Books
KB Articles
   HOW TO
   SAMPLE
   INFO
   BUG/PRB
   FIX
   Misc.
MSXML Tips
   August 2002
   September 2002
MSXML Tools

Microsoft XML Core Services


Go back to list of previously asked questions and answers

Question: I would like to include a PDF file as part of my XML document. I am using MSXML DOM, but am not able to figure out how would I include the PDF/DOC files (basically binary data) as part of my XML document (as a node value).

Updated:Thanks for posting the VB sample; however I was wondering if you could show some ASP code example?
Asked By: Sandra
Viewed: 13247
Answer: You can first read the binary file (such as PDF or DOC or XLS) into a byte array and let MSXML DOM encode it into Base64 format and make that as a node value. And then when you access the nodeTypedValue property of a node, MSXML will automatically convert the Base64 encoded data into the binary data. See the article XML and Binary Data for more help. We have also created a Visual Basic sample application that illustrates converting binary data into Base64, and back. Click here to download this sample application.



'Uses MSXML 4.0 SP1
'
'No Error handling is done
'
'Sample application to illustrate converting binary to XML (encode using Base64) and
'back from Base64 to binary
'
'Add project reference to MSXML 4.0

Private Sub btnEncode_Click()
    Dim objXMLDoc As New MSXML2.DOMDocument40
    Dim btArr() As Byte

    'The root node itslef will contain the base64 encoded data
    objXMLDoc.loadXML "<" & txtNodeName.Text & " />"
    
    'Read the PDF file into a byte array
    Open txtInputPDF.Text For Binary Access Read As #1
    
    ReDim btArr(LOF(1))
    Get #1, , btArr()
    Close #1
    
    With objXMLDoc.documentElement
        'Do base64 encoding
        .dataType = "bin.base64"
        .nodeTypedValue = btArr
    End With
    
    With txtOutputXMLFile
        'save the XML file
        objXMLDoc.save .Text
        MsgBox "XML file saved as :" & .Text
    End With
    
End Sub

Private Sub btnDecode_Click()
    Dim objXMLDoc As New MSXML2.DOMDocument40
    Dim btArr() As Byte
    
    objXMLDoc.async = False
    objXMLDoc.validateOnParse = False
    
    'in case loading from a HTTP server
    objXMLDoc.setProperty "ServerHTTPRequest", True
    
    'no error handling done here
    objXMLDoc.Load txtInputXML.Text
    
    'Decode the binary data and save into a byte array
    btArr = objXMLDoc.selectSingleNode("//" & txtDecodeNodeName.Text).nodeTypedValue
    
    'Save byte array into a file
    Open txtOutputPDF.Text For Binary Access Write As #1
    Put #1, 1, btArr
    Close #1
    
    MsgBox "PDF file saved as " & txtOutputPDF.Text
End Sub



Here is some sample ASP code:

The following page reads the PDF file on the server, does a base64 encoding using MSXML DOM, and sends the generated XML (that contains base64 encoded PDF file) to the response stream:

<%
	Option Explicit
	Dim objStream
    Dim objXMLDoc 
    
	Response.ContentType = "text/xml"

    Set objXMLDoc = Server.CreateObject("Msxml2.DOMDocument.4.0")

    'The root node itslef will contain the base64 encoded data
    objXMLDoc.loadXML "<Base64Data />"

	'Read the file into the stream
	Set objStream = Server.CreateObject("ADODB.Stream")
	objStream.Type = 1
	objStream.Open
	objStream.LoadFromFile Server.MapPath("Files/Chapter.PDF")

	
    'Do base64 encoding
    objXMLDoc.documentElement.dataType = "bin.base64"
    objXMLDoc.documentElement.nodeTypedValue = objStream.Read

	'Write the document on to the response stream
    objXMLDoc.save Response
%>


The following ASP page reads the XML file present on the server. This XML file contains the base64 encoded binary data. The ASP page convers the base64 data to binary and sends the binary data (an .gif file image) to the response stream (to the client):

<%
Option Explicit
Dim objXMLDoc 
	
Set objXMLDoc = Server.CreateObject("Msxml2.DOMDocument.4.0")
objXMLDoc.async = False
objXMLDoc.validateOnParse = True
	
'If you are loading remote XML Docs, uncomment the following line
'objXMLDoc.setProperty "ServerHTTPRequest", True
	
If objXMLDoc.load (Server.MapPath("bank.xml")) Then
		
	Dim SigNode
	Set SigNode = objXMLDoc.selectSingleNode("//SIGNATURE")
		
	If SigNode Is Nothing Then
		'Image not found!!
	Else
		Response.ContentType = "image/gif"
		Response.BinaryWrite SigNode.nodeTypedValue
	End If
Else
	'Do error handling using objXMLDoc.parseError
End If

%>


Go back to list of previously asked questions and answers
  Contact Us | E-mail Us | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy