Discussion Locked

Encode complex data with base64

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
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?

7 replies

by pete.thompson

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.

by tim.fortney

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?

by pete.thompson

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
Dim oNode As MSXML2.IXMLDOMNode

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

End Sub

by brian

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

by snoopdoug

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

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

mydoc.loadXML("");
docroot = mydoc.documentElement;

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

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

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[i] = (byte)fs.ReadByte();
}

newElem.nodeTypedValue = b;
docroot.appendChild(newElem);

MessageBox.Show(docroot.xml);
}
}
}

by rbates

Change the line
Get fd, filedata()

to
Get #fd, , filedata()

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

Thanks for all the articles!

by xml beginner

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.

This discussion is closed to new replies.