473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Serialize and deserialize

I got something like this in VB.Net

<Serializable()> Public Class YQProfileC
Inherits CollectionBase
Public Sub Save2File(ByVal FileName As String)
Dim IFormatter As New BinaryFormatter()
Dim FS As FileStream = New FileStream(FileName, FileMode.Create,
FileAccess.Write, FileShare.None)
IFormatter.Serialize(FS, Me)
FS.Close()
End Sub

Public Function LoadFromFile(ByVal FileName As String) As YQProfileC
Dim IFormatter As New BinaryFormatter()
Dim FS As FileStream = New FileStream(FileName, FileMode.Open,
FileAccess.Read, FileShare.None)
Return IFormatter.Deserialize(FS)
End Function
End Class

Now I got two problem here:
1.For some reason, the Root space name of the project needs to be changed
from YQV020 to YQV. But after that, how could I deserialize the old file.
2.What would have been done to deserialize the file if we want o change the
code from VB.net to C#.net? Please don't ask me why.

Thanks very much.
Amsteel
Nov 15 '05 #1
3 5855
Jax
Binary Serialization with C#

You will need:

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

Serializing Code:

string path = @"c:\SomeFolder\someFile.bin"
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(path, FileMode.Create,
FileAccess.Write, FileShare.None);
try
{
formatter.Serialize(stream, o);
}
finally
{
stream.Close();
}
}
catch(Exception)
{
MessageBox.Show("Problem with serialization please review
the path and ensure it is correct and exists. Path: " +
path, "Problem");
}
}

And deserializing code:

try
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(path, FileMode.Open,
FileAccess.Read, FileShare.Read);

object o = (object) formatter.Deserialize(stream);
stream.Close();
return o;

finally
{
stream.Close();
}
}
catch(Exception)
{
MessageBox.Show("General serialization error. Please check
legitmacy of path. Path: "+path, "Problem");
return new object();
}
}

Hope that helps
jax
-----Original Message-----
I got something like this in VB.Net

<Serializable()> Public Class YQProfileC
Inherits CollectionBase
Public Sub Save2File(ByVal FileName As String)
Dim IFormatter As New BinaryFormatter()
Dim FS As FileStream = New FileStream(FileName, FileMode.Create,FileAccess.Write, FileShare.None)
IFormatter.Serialize(FS, Me)
FS.Close()
End Sub

Public Function LoadFromFile(ByVal FileName As String) As YQProfileC Dim IFormatter As New BinaryFormatter()
Dim FS As FileStream = New FileStream(FileName, FileMode.Open,FileAccess.Read, FileShare.None)
Return IFormatter.Deserialize(FS)
End Function
End Class

Now I got two problem here:
1.For some reason, the Root space name of the project needs to be changedfrom YQV020 to YQV. But after that, how could I deserialize the old file.2.What would have been done to deserialize the file if we want o change thecode from VB.net to C#.net? Please don't ask me why.

Thanks very much.
Amsteel
.

Nov 15 '05 #2
Amsteel,
1.For some reason, the Root space name of the project needs to be changed
from YQV020 to YQV. But after that, how could I deserialize the old file. The following three part article covers binary serialization in detail:
http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

I believe the third one discusses what to do if the namespace changes.
2.What would have been done to deserialize the file if we want o change the code from VB.net to C#.net? Please don't ask me why. Changing from VB.NET to C# should not effect anything, as long as both types
are "exactly" the same. For the change in the 'name' (version number really)
you can use the code in the above articles.

Hope this helps
Jay

"Amsteel" <z2***@hotmail.com> wrote in message
news:MS*****************@news01.bloor.is.net.cable .rogers.com... I got something like this in VB.Net

<Serializable()> Public Class YQProfileC
Inherits CollectionBase
Public Sub Save2File(ByVal FileName As String)
Dim IFormatter As New BinaryFormatter()
Dim FS As FileStream = New FileStream(FileName, FileMode.Create,
FileAccess.Write, FileShare.None)
IFormatter.Serialize(FS, Me)
FS.Close()
End Sub

Public Function LoadFromFile(ByVal FileName As String) As YQProfileC
Dim IFormatter As New BinaryFormatter()
Dim FS As FileStream = New FileStream(FileName, FileMode.Open,
FileAccess.Read, FileShare.None)
Return IFormatter.Deserialize(FS)
End Function
End Class

Now I got two problem here:
1.For some reason, the Root space name of the project needs to be changed
from YQV020 to YQV. But after that, how could I deserialize the old file.
2.What would have been done to deserialize the file if we want o change the code from VB.net to C#.net? Please don't ask me why.

Thanks very much.
Amsteel

Nov 15 '05 #3
Thanks, it looks like I got a lot of reading to do.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Oo**************@tk2msftngp13.phx.gbl...
Amsteel,
1.For some reason, the Root space name of the project needs to be changed from YQV020 to YQV. But after that, how could I deserialize the old file.
The following three part article covers binary serialization in detail:
http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

I believe the third one discusses what to do if the namespace changes.
2.What would have been done to deserialize the file if we want o change the
code from VB.net to C#.net? Please don't ask me why.

Changing from VB.NET to C# should not effect anything, as long as both

types are "exactly" the same. For the change in the 'name' (version number really) you can use the code in the above articles.

Hope this helps
Jay

"Amsteel" <z2***@hotmail.com> wrote in message
news:MS*****************@news01.bloor.is.net.cable .rogers.com...
I got something like this in VB.Net

<Serializable()> Public Class YQProfileC
Inherits CollectionBase
Public Sub Save2File(ByVal FileName As String)
Dim IFormatter As New BinaryFormatter()
Dim FS As FileStream = New FileStream(FileName, FileMode.Create,
FileAccess.Write, FileShare.None)
IFormatter.Serialize(FS, Me)
FS.Close()
End Sub

Public Function LoadFromFile(ByVal FileName As String) As YQProfileC
Dim IFormatter As New BinaryFormatter()
Dim FS As FileStream = New FileStream(FileName, FileMode.Open,
FileAccess.Read, FileShare.None)
Return IFormatter.Deserialize(FS)
End Function
End Class

Now I got two problem here:
1.For some reason, the Root space name of the project needs to be

changed from YQV020 to YQV. But after that, how could I deserialize the old file. 2.What would have been done to deserialize the file if we want o change

the
code from VB.net to C#.net? Please don't ask me why.

Thanks very much.
Amsteel


Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ian Tompsett | last post by:
H I was wondering if it possible for an object to serialize/deserialize itself from XML. I'd be guessing that it would need to use the XmlSerializer class, but that seems to want to create a...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
0
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
1
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
2
by: alexandre martins | last post by:
Every time i try to make Deserialize the computer gives me the folowing error: "End of Stream encountered before parsing was complete" the code that i'm running is simple and is based on an...
2
by: Joe | last post by:
Hi I have a Generics List in a PropertyGrid I am able to Serialize it to XML but when I try to deserialize back to the class of the PropertyGrid The Constructor doesn't seem to fire to reload...
4
by: Hone | last post by:
I'm trying to serialize/deserialize the XML for an RSS 1.0 Feed (i.e. RDF) and the root element, as required by the standard, looks like this: <rdf:RDF ...> </rdf:RDF> However, I've tried...
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
4
by: djidan | last post by:
hi, i am a newbe to c#, i'm trying to send en custom object from a client to a server, after reading a lot on the web ifound that i need to use: BinaryFormatter, MemoryStream, and Serialize...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.