473,386 Members | 1,785 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,386 software developers and data experts.

serialization question

I have a binary serialized file on the client box. I would like to
open it with a completly different program to see what is in it. I
copied the the serialized class information from the project which
created it to a new project which will open the serialzed object. When
the program ran I got a run time error stating, I will not be able to
open the serialized object since it was created by a different dll or
namespace.
Is it possible to do this?
Thanks.
Jan 10 '08 #1
13 1438
Sure. Put the serializable class and the serializer class into a class
library, and use that in both projects.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

"DBC User" <db*****@gmail.comwrote in message
news:60**********************************@v29g2000 hsf.googlegroups.com...
>I have a binary serialized file on the client box. I would like to
open it with a completly different program to see what is in it. I
copied the the serialized class information from the project which
created it to a new project which will open the serialzed object. When
the program ran I got a run time error stating, I will not be able to
open the serialized object since it was created by a different dll or
namespace.
Is it possible to do this?
Thanks.

Jan 10 '08 #2
That's not going to work, since that serialization engine is going to
look at the type of the object that is serialized.

By moving the class file to another assembly, you are effectively
creating two separate types, and serialization will fail.

You could do this and create a ISurrogateSelector implementation which
will select the correct types to serialize/deserialize.

However, the easy way to do this would be to separate the class into a
separate assembly, and then reference the assembly from both programs. This
way, you have ^one^ definition of the type.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Kevin Spencer" <un**********@nothinks.comwrote in message
news:OA**************@TK2MSFTNGP06.phx.gbl...
Sure. Put the serializable class and the serializer class into a class
library, and use that in both projects.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

"DBC User" <db*****@gmail.comwrote in message
news:60**********************************@v29g2000 hsf.googlegroups.com...
>>I have a binary serialized file on the client box. I would like to
open it with a completly different program to see what is in it. I
copied the the serialized class information from the project which
created it to a new project which will open the serialzed object. When
the program ran I got a run time error stating, I will not be able to
open the serialized object since it was created by a different dll or
namespace.
Is it possible to do this?
Thanks.


Jan 10 '08 #3
On Thu, 10 Jan 2008 08:22:36 -0800, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
[...]
However, the easy way to do this would be to separate the class into
a
separate assembly, and then reference the assembly from both programs.
This
way, you have ^one^ definition of the type.
This is, as near as I can tell, what Kevin meant when he wrote "into a
class library".

I didn't read his post to mean copying the code for the serialized class
into each place it will be used, but rather to move it into a separate
assembly just as you've suggested here.

Pete
Jan 10 '08 #4
Yeah, I read it too fast, I read it as putting the class in each
assembly.

- Nick

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 10 Jan 2008 08:22:36 -0800, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
>[...]
However, the easy way to do this would be to separate the class into
a
separate assembly, and then reference the assembly from both programs.
This
way, you have ^one^ definition of the type.

This is, as near as I can tell, what Kevin meant when he wrote "into a
class library".

I didn't read his post to mean copying the code for the serialized class
into each place it will be used, but rather to move it into a separate
assembly just as you've suggested here.

Pete

Jan 10 '08 #5
On Jan 10, 2:11*pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
* * Yeah, I read it too fast, I read it as putting the class in each
assembly.

* * * * - Nick

"Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote in message

news:op***************@petes-computer.local...
On Thu, 10 Jan 2008 08:22:36 -0800, Nicholas Paldino [.NET/C# MVP]
<m...@spam.guard.caspershouse.comwrote:
[...]
* * However, the easy way to do this would be to separate the classinto
a
separate assembly, and then reference the assembly from both programs.
This
way, you have ^one^ definition of the type.
This is, as near as I can tell, what Kevin meant when he wrote "into a
class library".
I didn't read his post to mean copying the code for the serialized class
into each place it will be used, but rather to move it into a separate
assembly just as you've suggested here.
Pete- Hide quoted text -

- Show quoted text -
Thanks all for the answer. In this 'particular' situation I need a way
to read it without mixing and creating a dll/lib to use it. Sorry.
This is a very peculiar situation. Hopefully as suggested
ISurrogateSelector might help me. I never used it. I need to google it
to find out more about it. If any one has any sample code, please let
me know otherwise thank you for the comments and suggestions.
Jan 10 '08 #6
I have to ask, why can't you use a separate assembly dll? It seems like
a silly requirement.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"DBC User" <db*****@gmail.comwrote in message
news:ec**********************************@1g2000hs l.googlegroups.com...
On Jan 10, 2:11 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Yeah, I read it too fast, I read it as putting the class in each
assembly.

- Nick

"Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote in message

news:op***************@petes-computer.local...
On Thu, 10 Jan 2008 08:22:36 -0800, Nicholas Paldino [.NET/C# MVP]
<m...@spam.guard.caspershouse.comwrote:
[...]
However, the easy way to do this would be to separate the class into
a
separate assembly, and then reference the assembly from both programs.
This
way, you have ^one^ definition of the type.
This is, as near as I can tell, what Kevin meant when he wrote "into a
class library".
I didn't read his post to mean copying the code for the serialized class
into each place it will be used, but rather to move it into a separate
assembly just as you've suggested here.
Pete- Hide quoted text -

- Show quoted text -
Thanks all for the answer. In this 'particular' situation I need a way
to read it without mixing and creating a dll/lib to use it. Sorry.
This is a very peculiar situation. Hopefully as suggested
ISurrogateSelector might help me. I never used it. I need to google it
to find out more about it. If any one has any sample code, please let
me know otherwise thank you for the comments and suggestions.
Jan 10 '08 #7
On Thu, 10 Jan 2008 12:24:54 -0800, DBC User <db*****@gmail.comwrote:
Thanks all for the answer. In this 'particular' situation I need a way
to read it without mixing and creating a dll/lib to use it. Sorry.
I agree with Nicholas...that seems like a silly requirement.

However, if that is indeed your requirement, you'll have to write your own
serialization, something that's not tied to the .NET class the way the
built-in is.

Pete
Jan 10 '08 #8
One cheap option here - use xml serialization. DataContractSerializer,
for example, can be used with "preserver references" enabled to
properly serialize the object *graph* (not the object *tree* that
XmlSerializer would produce) - just wihout the assembly information
that is plaguing you. It would also be trivial to parse, transform,
etc.

If you are concerned about space (for big graphs), then another easy
trick is to chain a GZipStream.

An illustration (demonstrating DataContract working on a graph with
loops, and working through GZipStream):

[DataContract]
public class MyData
{
// simple values
[DataMember]
public int SomeValue { get; set; }
[DataMember]
public string SomeOtherValue { get; set; }

// to prove object graph
[DataMember]
public MyData Sibling { get; set; }
}
class Program
{
static void Main()
{
MyData a = new MyData { SomeValue = 4, SomeOtherValue =
"abc" },
b = new MyData { SomeValue = 6, SomeOtherValue =
"def", Sibling = a };
a.Sibling = b; // make a closed loop...

DataContractSerializer dcs = new DataContractSerializer(
typeof(MyData), null, int.MaxValue, true, true, null);

// write
using (Stream zip = new GZipStream(
File.Create("test.bin"), CompressionMode.Compress))
{
dcs.WriteObject(zip, a);
zip.Close();
}

// read
MyData c;
using (Stream zip = new GZipStream(
File.OpenRead("test.bin"),
CompressionMode.Decompress))
{
c = (MyData)dcs.ReadObject(zip);
}

Console.WriteLine("{0}, {1}\t{2}, {3}\t{4}",
c.SomeValue, c.SomeOtherValue,
c.Sibling.SomeValue, c.Sibling.SomeOtherValue,
ReferenceEquals(c, c.Sibling.Sibling));

}
}
Jan 10 '08 #9
I forgot to say; the point of a DataContract is that you can load it
simply with a matching contract (i.e. copy/paste) - it doesn't care
about specific assemblies etc.

Marc
Jan 10 '08 #10
And the XML will not work because the file is ^already^ serialized from
another program.

Using a DataContractSerializer ^might^ work, as the
DataContractSerializer does honor classes that are tagged with Serializable
and follows the same semantics as it, but there is no guarantee that the
format will be compatable with what the BinaryFormatter produces (as a
matter of fact, I'd be very surprized if it did).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marc Gravell" <ma**********@gmail.comwrote in message
news:f9**********************************@v4g2000h sf.googlegroups.com...
>I forgot to say; the point of a DataContract is that you can load it
simply with a matching contract (i.e. copy/paste) - it doesn't care
about specific assemblies etc.

Marc

Jan 10 '08 #11
[Nicholas Paldino]And the XML will not work because the file is
^already^ serialized from another program.

Ah - right, misunderstood. Yes, if the OP *already* has the serialized
file, then, well, it gets very tricky.

My reply was to "if I am /designing/ a system that (as a requirement)
must be able to do this..." - in which case a data-contract is a
perfectly viable solution - but no, it absolutely won't allow you to
read output from BinaryFormatter...

But then, the same applies to your own suggestion of using a shared
assembly from both projects - it is presumably already too late to
change the one that produced the existing file.

Marc
Jan 10 '08 #12
On Jan 10, 4:13*pm, Marc Gravell <marc.grav...@gmail.comwrote:
[Nicholas Paldino]And the XML will not work because the file is
^already^ serialized from another program.

Ah - right, misunderstood. Yes, if the OP *already* has the serialized
file, then, well, it gets very tricky.

My reply was to "if I am /designing/ a system that (as a requirement)
must be able to do this..." - in which case a data-contract is a
perfectly viable solution - but no, it absolutely won't allow you to
read output from BinaryFormatter...

But then, the same applies to your own suggestion of using a shared
assembly from both projects - it is presumably already too late to
change the one that produced the existing file.

Marc
Thank you very much again. Since you have asked the requirement, here
is what is happening. We have a massive project which does all these
work and I do have these serialised object in a seperate dll. Now I
got a special request from one of a biggest client, they want to
update this serialised object in all their install with specific
information. I do have an option on the GUI to update the serialised
object. But client said, users tend to make mistake and the IT
department want to update it through back door, by asking all the
users in the company to run a program from network and 'this' program
will read the clients serialised object and update it.
So here is what I went about, I created a program referenced the dll
to update the object and everything worked ok. I signed the
application with the certificate and ran a test in one of our server,
dll failed with security exception stating the dll doesn't have enough
security and has to be given full trust. Now I ran a caspol on it
manually and it worked. I could have added the caspol command in a
batch program and let the client run the batch program, where the
caspol runs first then run the actual program. But the server location
where I need to point to the dll can not be hardcoded since there are
multiple server location the clients could point to. So I thought, if
somehow I can access the serialised object through the main program
instead of the dll I could resolve the problem without running caspol.
This is my story :(
Jan 11 '08 #13
On Jan 10, 10:21 pm, DBC User <dbcu...@gmail.comwrote:
This is my story :(
Assuming you already have the serialized file but you don't have
access to the DLL that created it, your only real option is to write a
tool that parses the binary file directly! Yuck.

I have written a similar tool in the past... I HACKED the binary file
to change the values by using the BinaryReader and BinaryWriter class.

I hate this type of code... I despise it, really... but sometimes it
is the only way.

B
Jan 11 '08 #14

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

Similar topics

16
by: Bob Rock | last post by:
Hello, when serializing an array of elements of a class Classname using XmlSerializer.Serialize() I get an XML like the following: <?xml version="1.0"> <ArrayOfClassname> ....... ..........
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
0
by: eSapient | last post by:
I generated serialization/deserialization code for this schema using the xsd tool: <?xml version="1.0" encoding="UTF-8"?> <xs:schema elementFormDefault="qualified"...
6
by: John Glover | last post by:
I'm having a very strange problem with XML serialization. I'm writing web services which pass instances of various classes back and forth as parameters and return values of web methods. The...
15
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with...
0
by: groovyghoul | last post by:
Hi I have the following XML file: =========================================================== <?xml version="1.0" encoding="UTF-16"?> <Policy xmlns="http://tempuri.org/richard.xsd"> <TransType...
0
by: nobin01 | last post by:
Dear sir; I want ur Help in serialization.I know serialization.I Know binary,soap and xmlserialization also.But i want ur help in following topics.pls help me as soon as possible.I have search in...
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
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,...
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,...

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.