Hello!
Assume I have a class called Product which is defined with the attribute
[Serializable]
I create a collection by using the generic class List in this way
List<Productpro ducts = new List<Product>() ;
product.Add(new Product(1, "some name 1",120));
product.Add(new Product(1, "some name 2",130));
product.Add(new Product(1, "some name 3",140));
IFormatter serializer = new BinaryFormatter ();
FileStream saveFile = new FileStream("Pro ducts.bin", FileMode.Create ,
FileAccess.Writ e);
serializer.Seri alize(saveFile, products);
saveFile.Close( );
Now to my question.
When I want to deserialize do I then have to use the same kind of generic
construction
with List<Productsin the way shown below or can I use other construction.
FileStream loadFile = new FileStream("Pro ducts.bin", FileMode.Open,
FileAccess.Read );
List<Productsav edProducts =
serializer.Dese rialize((List<P roduct>)loadFil e);
loadFile.Close( );
My second question:
I'm reading in a book and they say the following.
"Some object don't serialize very well. They may require reference to local
data that only exist
while they are in memory, for example."
What does this mean.
//Tony 1 1762
Well, move the cast and that is about right:
List<Productsav edProducts = (List<Product>
serializer.Dese rialize(loadFil e);
Personally, I might be tempted to consider an xml-based serializer
(such as XmlSerializer) since it will make fewer demands for specific
assemblies, etc.
Re some objects not serializing very well - this could mean references
to non-serializable things like an SqlConnection, or it could mean
things like unmanaged pointers / handles: these are essentially just
numbers, but are useless at a different time, etc.
Marc This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 brand new object when deserializing. In my case I have an existing object that I'd like to pass some XML to for the object to repopulate its member variables. Similarly I'd like it to be able to populate an XML string from the values of its member...
|
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
|
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 s);
public void Deserialize(Stream s);
Within the MyUserControl class, there is a field of type MyInnerClass
|
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 and properties derived from
this class but will not serialize or deserialize the properties in
CollectionBase. Like InnerList, which is a read only property of
CollectionBase.
How can I serialize and deserialize the InnerList property of...
|
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 and properties derived from
this class but will not serialize or deserialize the properties in
CollectionBase. Like InnerList, which is a read only property of
CollectionBase.
How can I serialize and deserialize the InnerList property of...
| |
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
MSDN example. The CODE is BELOW this lines. If you see
something wrong or missing please answer.
Class declaration:
|
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 the saved settings
Can anyone see something that I have missed ?
|
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 everything using XmlRootAttribute, XmlTypeAttribute,
XmlSerializerNamespacesAttribute, etc, and nothing will serialize to or
deserialize from this root element format.
|
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 Building._Name to Building._BName
in case the duplicate name was the issue, but that didn't help.
Is there a native way to serialize nested objects, or will I have to write
my own?
public class LocationCell
|
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 Function, and Deserialize Function,
but the thing is that when trying to Serialize and then Deserialize on the same project it's working perfectly, but when i send the message to the server and trying to Deserialize i get en exception "Unable to find...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |