Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > Pro VB.NET 2002/2003
|
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 7th, 2004, 05:11 AM
Authorized User
 
Join Date: Dec 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to eelisMX
Default File to binary and save as string

Hi

I'm trying to convert a file to binary and then save it as string.
Later I would like to take the string and regenerate the file out of it, on a different station.


Thanks a lot!

Eelis
 
Old December 7th, 2004, 07:53 AM
Registered User
 
Join Date: Jan 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Eelis,

.Net provides Serialization & Deserialization of objects using formatters
Serialization and deserialization is performed by an object that implements the IFormatter interface. The .NET Framework Class Library (FCL) provides two implementations of this interface, BinaryFormatter and SoapFormatter.
In your case , a BinaryFormatter needs to be used to convert the text file to binary.

Cheers,
Monica

Cheers,
Monica D'Souza
 
Old December 7th, 2004, 08:49 AM
Authorized User
 
Join Date: Dec 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to eelisMX
Default

Thanks!
I figured out I have to use one of them (for my purpose its better to use the SoapFormatter).

Could you be more specific in the serialization process (if possible with a short example of serializing a file).


Thanks again!
 
Old December 8th, 2004, 06:04 AM
Registered User
 
Join Date: Jan 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can use the SoapFormatter just like the BinaryFormatter and it will work. The only thing you have to do is to add a reference of System.Runtime.Serialization.Formatters.Soap to get access to the Soapformatter(besides doing the same using the Imports keyword)


What is Serialization?
----------------------
Well, Serialization is the process of taking an object and converting it to a format
in which it can be transported across a network or persisted to a storage location. The storage location could be as simple as using a file or a database.

The serialized format contains the object's state information.
Deserialization is the process wherein you reconstruct an object from the serialized state to its original state. In essence, the process of serialization allows an object to be serialized, shipped across the network for remoting or persisted in a storage location such as the ASP.NET cache, and then be reconstructed for use at a later point in time.

There are three formats provided by the Microsoft .NET framework to which objects can be serialized. The formats are binary, SOAP, and XML. The XML format is produced by using the System.Xml.Serialization.XmlSerializer class. The SOAP and binary formats are produced by using classes under the System.Runtime.Serialization.Formatters namespace.

Here's the sample code you asked for. Hope this helps!

Sample code:
------------
Imports System.Runtime.Serialization.Formatters.Soap
Imports System.IO

Public Class Class1

    Public Sub SoapSerialize()
        'Create a streamreader class
        Dim objStreamReader As StreamReader
        'open the file
        objStreamReader = File.OpenText("c:\\Mystore1.txt")
        'Read the file storing it in the readfile string
        Dim readfile As String = objStreamReader.ReadToEnd()
        Dim flStream As FileStream = New FileStream("c:\\myStore.txt", FileMode.OpenOrCreate, FileAccess.Write)
        Try
            Dim sFormatter As SoapFormatter = New SoapFormatter
            sFormatter.Serialize(flStream, readfile)
        Finally
            flStream.Close()
            objStreamReader.Close()
        End Try
    End Sub


Cheers,
Monica
 
Old December 8th, 2004, 10:26 AM
Authorized User
 
Join Date: Dec 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to eelisMX
Default

That's just it!!!

Thanks for the invaluable assistance.

Eelis





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to save an OLE Object to Long Binary being_oblivious VB How-To 0 August 1st, 2005 11:53 PM
Save BINARY FILE in ACCESS database gasma1975 Access 4 February 10th, 2005 12:16 PM
How to save string value as html file? android66 Javascript 0 September 3rd, 2004 12:00 AM
decimal string to binary se7ss C# 5 July 19th, 2004 02:52 PM
String to Binary, Binary to String matt.fields VB.NET 2002/2003 Basics 3 November 5th, 2003 05:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.