Web Development

General discussion


Encode complex data with base64

By Mark W. Kaelin Editor ·
The Jan. 15, XML TechMail discusses the storing of complex data within an XML document and the problems that can cause. The use of base64 encoding is one solution to the problem; can you suggest other methods?

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -


by pete.thompson In reply to Encode complex data with ...

The code snippet references 'Getfile', which causes the VB code to error (Sub or Function not defined). What do I need to do to get this to work. Please help.

Collapse -

Would be nice

by tim.fortney In reply to Getfile??

would be nice to see the complete code with functions and subs.

It would also be nice to see how you extract the binary data from the xml file.

Anyone have suggestions?

Collapse -

Decoding back to binary

by pete.thompson In reply to Would be nice

Code below requires a simple form with input xml and output file text boxes and button.

Private Sub btnOK_Click()
Dim oXMLDOMDoc As New MSXML2.DOMDocument40

oXMLDOMDoc.async = False

If oXMLDOMDoc.Load(txtInputXMLFile.Text) Then
Set oNode = oXMLDOMDoc.selectSingleNode("Person/Photo")

Dim btArr() As Byte
btArr = oNode.nodeTypedValue

Open txtOutputImageFile.Text For BinaryAs #1
Put #1, 1, btArr
Close #1

Set oXMLDOMDoc = Nothing
Set oNode = Nothing

MsgBox "Photo saved into the bmp file with the name " & txtOutputImageFile.Text

Else MsgBox "Error: " & oXMLDOMDoc.parseError.reason
End If


End Sub

Collapse -

How to display binary data in browser?

by XML Beginner In reply to Decoding back to binary

It seems the suggested code is in VB. I tried to convert it to VBScript so that the XML file (with the binary files inside) can be displayed in the browser. However, I have problem in writing the binary files in local machine by using VBScript due to the security reason. Is there any suggestion?

Thanks in advance.

Collapse -

How to getfile()

by brian In reply to Encode complex data with ...

Thanks to those of you who read the article and would like to "make it work". Here's a short implementation of the getfile() function that is referenced in the article, which should help get you started.

This function simply opens a binary file,allocates a byte array to the size of the file, and reads the data into the byte array. The functions returns the data in the byte array.

Best regards,

Brian Schaffner

Private Function getfile(FileName as String) as Variant
Dim filedata() as Byte
Dim FileLength as Long
Dim fd as Integer
fd = FreeFile

Open FileName For Binary as #fd
FileLength = LOF(fd)
ReDim filedata(0 to FileLength - 1)
Get fd, filedata()
Close #fd

getfile = filedata
End Function

Collapse -

Here's the c# version

by SnoopDoug In reply to How to getfile()

using System;
using System.IO;
using System.Windows.Forms;
using MSXML2;

namespace XmlBin
class Class1
static void Main(string[] args)
DOMDocument40Class mydoc =
new DOMDocument40Class();
IXMLDOMElement docroot;
IXMLDOMElement newElem;

docroot = mydoc.documentElement;

newElem = mydoc.createElement("Name");
newElem.text = "John Doe";

newElem = mydoc.createElement("Age");
newElem.text = "33";

newElem = mydoc.createElement("Photo");
newElem.set_dataType( "bin.base64" );
FileStream fs = new FileStream("C:\\photo.bmp",
FileMode.Open, FileAccess.Read);

long l = fs.Length;
byte [] b = new byte[l];

for(long i = 0; i < l; i++)
b = (byte)fs.ReadByte();

newElem.nodeTypedValue = b;


Collapse -

Error still in "Get" call

by rbates In reply to How to getfile()

Change the line
Get fd, filedata()

Get #fd, , filedata()

There is also a FSO Getfile routine which further confused [me] on the issue!

Thanks for all the articles!

Related Discussions

Related Forums