472,975 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Serialize A Dictionary

I need to serialize a dictionary so I can encode the contents. I have the
following working but the size seems large. I am guessing that I am
serializing the entire object not just the data. Is there a better way?

MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();

Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("name", "andrew");
dictionary.Add("home", "bellingham");

formatter.Serialize(stream, dictionary);
byte[] b = stream.ToArray();

How do I use "dictionary.GetObjectData()" ?

Thanks,
Dec 2 '05 #1
5 46936
Andrew,

You don't use GetObjectData. This is a method on the ISerializable
interface that is implemented to provide custom serialization semantics.

You aren't going to be able to adjust the size of the output of
serialization. Yes, you are serializing the entire object, which means the
keys and values. If you need to serialize just the values, get the
ICollection implementation that returns the values (through the Values
property) and serialize that.

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

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...
I need to serialize a dictionary so I can encode the contents. I have the
following working but the size seems large. I am guessing that I am
serializing the entire object not just the data. Is there a better way?

MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();

Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("name", "andrew");
dictionary.Add("home", "bellingham");

formatter.Serialize(stream, dictionary);
byte[] b = stream.ToArray();

How do I use "dictionary.GetObjectData()" ?

Thanks,

Dec 2 '05 #2
Nicholas,

Thanks, but serializing dictionary.Values actually produces a larger stream.

I am thinking that I might have to roll my own very lightweight class.

-Andrew

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uR*************@TK2MSFTNGP10.phx.gbl...
Andrew,

You don't use GetObjectData. This is a method on the ISerializable
interface that is implemented to provide custom serialization semantics.

You aren't going to be able to adjust the size of the output of
serialization. Yes, you are serializing the entire object, which means
the keys and values. If you need to serialize just the values, get the
ICollection implementation that returns the values (through the Values
property) and serialize that.

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

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...
I need to serialize a dictionary so I can encode the contents. I have the
following working but the size seems large. I am guessing that I am
serializing the entire object not just the data. Is there a better way?

MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();

Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("name", "andrew");
dictionary.Add("home", "bellingham");

formatter.Serialize(stream, dictionary);
byte[] b = stream.ToArray();

How do I use "dictionary.GetObjectData()" ?

Thanks,


Dec 2 '05 #3
Andrew,

Why not just compress the stream, if you are that concerned about it.

How many bytes are we talking about here? Why such a concern?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:uW**************@TK2MSFTNGP11.phx.gbl...
Nicholas,

Thanks, but serializing dictionary.Values actually produces a larger
stream.

I am thinking that I might have to roll my own very lightweight class.

-Andrew

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:uR*************@TK2MSFTNGP10.phx.gbl...
Andrew,

You don't use GetObjectData. This is a method on the ISerializable
interface that is implemented to provide custom serialization semantics.

You aren't going to be able to adjust the size of the output of
serialization. Yes, you are serializing the entire object, which means
the keys and values. If you need to serialize just the values, get the
ICollection implementation that returns the values (through the Values
property) and serialize that.

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

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...
I need to serialize a dictionary so I can encode the contents. I have the
following working but the size seems large. I am guessing that I am
serializing the entire object not just the data. Is there a better way?

MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();

Dictionary<string, string> dictionary = new Dictionary<string,
string>();
dictionary.Add("name", "andrew");
dictionary.Add("home", "bellingham");

formatter.Serialize(stream, dictionary);
byte[] b = stream.ToArray();

How do I use "dictionary.GetObjectData()" ?

Thanks,



Dec 2 '05 #4
Ideally, I would like to pass my stream in a URL. Even with a few name value
pairs, I am up around 2000 bytes and that is before I encrypt it with the
rijndael managed provider.

Maybe there is a better approach to this whole thing. I need to pass a
number of name value pairs. I just don't know at design time how many pairs
or even what the name are. Typically 2 to 4. Originally, I thought that a
dictionary was my best approach.

Thanks,

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
Andrew,

Why not just compress the stream, if you are that concerned about it.

How many bytes are we talking about here? Why such a concern?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:uW**************@TK2MSFTNGP11.phx.gbl...
Nicholas,

Thanks, but serializing dictionary.Values actually produces a larger
stream.

I am thinking that I might have to roll my own very lightweight class.

-Andrew

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:uR*************@TK2MSFTNGP10.phx.gbl...
Andrew,

You don't use GetObjectData. This is a method on the ISerializable
interface that is implemented to provide custom serialization semantics.

You aren't going to be able to adjust the size of the output of
serialization. Yes, you are serializing the entire object, which means
the keys and values. If you need to serialize just the values, get the
ICollection implementation that returns the values (through the Values
property) and serialize that.

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

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...
I need to serialize a dictionary so I can encode the contents. I have
the following working but the size seems large. I am guessing that I am
serializing the entire object not just the data. Is there a better way?

MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();

Dictionary<string, string> dictionary = new Dictionary<string,
string>();
dictionary.Add("name", "andrew");
dictionary.Add("home", "bellingham");

formatter.Serialize(stream, dictionary);
byte[] b = stream.ToArray();

How do I use "dictionary.GetObjectData()" ?

Thanks,



Dec 2 '05 #5
Andrew,

Trying to send any significant amount of information through the URL is
not a good idea, mainly because of the limitation on the URL size.

A better idea would be to POST the data, and then have your ASPX page
(or whatever is processing it on the other side), deserialize it.

Is it .NET that is working on the other side?

Have you considered a web service in this case? It would be much better
suited for what you want to do.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:uj**************@TK2MSFTNGP10.phx.gbl...
Ideally, I would like to pass my stream in a URL. Even with a few name
value pairs, I am up around 2000 bytes and that is before I encrypt it
with the rijndael managed provider.

Maybe there is a better approach to this whole thing. I need to pass a
number of name value pairs. I just don't know at design time how many
pairs or even what the name are. Typically 2 to 4. Originally, I thought
that a dictionary was my best approach.

Thanks,

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP14.phx.gbl...
Andrew,

Why not just compress the stream, if you are that concerned about it.

How many bytes are we talking about here? Why such a concern?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:uW**************@TK2MSFTNGP11.phx.gbl...
Nicholas,

Thanks, but serializing dictionary.Values actually produces a larger
stream.

I am thinking that I might have to roll my own very lightweight class.

-Andrew

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:uR*************@TK2MSFTNGP10.phx.gbl...
Andrew,

You don't use GetObjectData. This is a method on the ISerializable
interface that is implemented to provide custom serialization
semantics.

You aren't going to be able to adjust the size of the output of
serialization. Yes, you are serializing the entire object, which means
the keys and values. If you need to serialize just the values, get the
ICollection implementation that returns the values (through the Values
property) and serialize that.

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

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...
>I need to serialize a dictionary so I can encode the contents. I have
>the following working but the size seems large. I am guessing that I am
>serializing the entire object not just the data. Is there a better way?
>
> MemoryStream stream = new MemoryStream();
> BinaryFormatter formatter = new BinaryFormatter();
>
> Dictionary<string, string> dictionary = new Dictionary<string,
> string>();
> dictionary.Add("name", "andrew");
> dictionary.Add("home", "bellingham");
>
> formatter.Serialize(stream, dictionary);
> byte[] b = stream.ToArray();
>
> How do I use "dictionary.GetObjectData()" ?
>
> Thanks,
>



Dec 2 '05 #6

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

Similar topics

1
by: Benton | last post by:
Hi there, I could serialize a class until I added this member to it: private Dictionary<string><TableMapper> = new Dictionary<string><TableMapper>(); The class has the attribute, but now...
0
by: Tim_Mac | last post by:
hi, i am able to serialize a normal generic List<> no problem. But when i try to serialize a Dictionary<string,string>, i get an exception as shown below: "The type...
1
by: Gabe Covert | last post by:
I have the following classes defined (gets/sets removed for brevity): public class Warband { public Warband() { Contents = new Dictionary<string, WarbandContent>(); }
3
by: fstorck | last post by:
Hi, I'm kind of stuck with an serializing / deserializing problem using a generic dictionary holding references to various generic types. It goes as follows: <code> class MyBase :...
0
by: =?Utf-8?B?S2V2aW4gU2NobmVpZGVy?= | last post by:
I have two projects (one using .NET 1.1 the other using 2.0). When I call a particular method in a webservice from the 1.1 project everything works great. When I call the same method from the 2.0...
1
by: not_a_commie | last post by:
So I thought I saw recently some new MS serializer class (part of workflow foundation or something) that would serialize a lot of types the old XmlSerializer would not do. This included generic...
2
by: Andy B | last post by:
Is it possible to serialize to xml a dictionary<string, stringobject in ..net 3.5?
2
by: =?Utf-8?B?U2hhd24=?= | last post by:
Hi; I would like to be able to use the XMLSerializer to serialize and deserialize a dictionary. is that possible? i know that you can serialize an object that implements the ICollection interface....
2
by: Andy B | last post by:
I don't know if this is even working or not but here is the problem. I have a gridview that I databound to a dictionary<string, stringcollection: Contract StockContract = new Contract();...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.