Click to See Complete Forum and Search --> : Storing binary file inside XML


brouse
06-13-2007, 10:29 AM
I want to convert a binary file into a data that can be stored in a node in an XML file, and then reconstitute it back to a file later. I am using the binary reader and writer and it almost works, except the very first byte is getting changed. I'm also thinking there has to be an easier way to do it.

I could post my code, but considering WebServices and SOAP, it seems that this sort of thing should be trivial in .NET, so I'll ask first can someone point me in the direction of a good example for doing this sort of thing?

Thanks...

brouse
06-13-2007, 03:40 PM
I found the solution (with help from a different forum). For posterity (in case someone else stumbles across this thread with the same problem), here is what I ended up with:

Private Function FileToBLOB(ByVal sFilePath As String) As String

Dim aFile() As Byte = {}
Dim sFile As String = ""

Try

Dim oFileStream As New IO.FileStream(sFilePath, IO.FileMode.Open)
Dim oBinaryReader As New IO.BinaryReader(oFileStream)

aFile = oBinaryReader.ReadBytes(CInt(oFileStream.Length))
sFile = Convert.ToBase64String(aFile)


Return sFile

Catch ex As Exception
MsgBox("Error converting file content to XML: " & ex.Message)
Return ""
End Try

End Function

Private Function BLOBToFile(ByVal sFileData As String, ByVal sFilePath As String) As Boolean

Dim aFile() As Byte = {}

Try

Dim oFileStream As New IO.FileStream(sFilePath, IO.FileMode.OpenOrCreate)
Dim oBinaryWriter As New IO.BinaryWriter(oFileStream)

aFile = Convert.FromBase64String(sFileData)
oBinaryWriter.Write(aFile)

Catch ex As Exception
MsgBox("Error reconstituting file from the XML data: " & ex.Message)
Return False
End Try

Return True

End Function