473,326 Members | 2,081 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,326 software developers and data experts.

Serialize Resource

427 Expert 256MB
Hi guys,

C# .Net 2.0 or later, Win App.

so this is a bit of a strange question: i would like to serialize (to binary) an embedded resource of my project.

I normally perform serialization via a binary formatter using a file stream.
I have problems using the resource as a parameter to the filestream constructor as it wants a string/handle while my resource is a byte[] casting to a string/handle also doesn't seam to solve the problem.

I tried creating a serializable class which holds a only a string. But again i am not able to read the resource using a stream so that i may fill and then serialize that class.

Am i on the wrong track? is there a better approach or is this just not possible?

Thanks in advance
Sep 11 '08 #1
1 1643
PRR
750 Expert 512MB
"so this is a bit of a strange question: i would like to serialize (to binary) an embedded resource of my project."

try and specify wat it is...

"I normally perform serialization via a binary formatter using a file stream.
I have problems using the resource as a parameter to the filestream constructor as it wants a string/handle while my resource is a byte[] casting to a string/handle also doesn't seam to solve the problem."

"I tried creating a serializable class which holds a only a string. But again i am not able to read the resource using a stream so that i may fill and then serialize that class."

i normally use a class to do serializing..here is a sample code with byte array..

Expand|Select|Wrap|Line Numbers
  1.  
  2. using System.IO;
  3. using System.Xml.Serialization;
  4.  
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.Runtime.Serialization;
  7.  
  8. public Program Dese(Program now)
  9.         {
  10.             XmlSerializer serial = new XmlSerializer(typeof(Program));
  11.  
  12.             BinaryFormatter formatter = new BinaryFormatter();
  13.  
  14.             //using (FileStream str = new FileStream(@"C:\byte.xml", FileMode.Open))
  15.             //{
  16.             //    now = (Program)serial.Deserialize(str);
  17.             //    //str.Dispose();
  18.             //}
  19.  
  20.             using(FileStream str=new FileStream(@"C:\byte.123",FileMode.Open))
  21.             {
  22.                 now=(Program)formatter.Deserialize(str);
  23.             }
  24.  
  25.             return now;
  26.         }
  27.         public void serailize(Program now)
  28.         {
  29.             XmlSerializer serialize = new XmlSerializer(typeof(Program));
  30.             using (TextWriter writer = new StreamWriter(@"C:\byte.xml"))
  31.             {
  32.                 serialize.Serialize(writer, now);
  33.  
  34.                 writer.Dispose();
  35.             }
  36.  
  37.             BinaryFormatter formatter = new BinaryFormatter();
  38.  
  39.             using (FileStream str = new FileStream(@"C:\byte.123", FileMode.OpenOrCreate))
  40.             {
  41.                 formatter.Serialize(str,now);
  42.             }
  43.  
  44.  
  45.         }
  46.         public static byte[] StrToByteArray(string str)
  47.         {
  48.  
  49.             System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  50.             return encoding.GetBytes(str);
  51.         }
  52.  
  53.  
  54. ///
  55.  
  56. [Serializable]
  57.     public class Program
  58.     {
  59.         public string ss = "prr";
  60.  
  61.         public byte[] str;
  62.         static void Main(string[] args)
  63.         {
  64.             Program p = new Program();
  65.             p.str=Program.StrToByteArray("prr");
  66.  
  67.             p.serailize(p);
  68.  
  69.             Program c;
  70.  
  71.             c=p.Dese(p);
  72.             Console.ReadLine();
  73.         }
  74.         /*
  75.          * byte [] dBytes = ...
  76.            string str;
  77.            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  78.            str = enc.GetString(dBytes);
  79.  
  80.          */
  81.  
  82.  
i dint get any problem... though i have used byte array that much... i guess its pretty much same
Sep 11 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

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...
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
3
by: MAY | last post by:
Hi, I have a problem about serialize the form controls. I wrote a test program to test serialize a from but fail (->An unhandled exception of type...
8
by: cd~ | last post by:
I can provide a test app, the news server won't allow me to post the files because they are too large (93KB and 1.2KB) I downloaded the ESRI ArcXml schema and generated the classes from the...
1
by: Tim | last post by:
Could anyone tell me what this means and how do I correct it. Any suggestions? Thanks! Tim Richardson IT Developer and Consultant www.paladin3d.com Unable to serialize the session state. In...
4
by: Abi | last post by:
We able to generate this error in our test environment and were able to research this enough to understand that the issue is NOT with an abject that needs to be serialized but rather as the stack...
0
by: Richard Lewis Haggard | last post by:
In an attempt to get around a resource leak issue, I tried to serialize a Cursor to a MemoryStream but the serialization fails with "Stock cursors cannot be serialized." This is puzzling since the...
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...
9
by: Gillard | last post by:
i get an exeption and i do not know what else to do to continue Dim sdf As New SaveFileDialog With sdf .AddExtension = True .DefaultExt = ".record" .FileName = Now.ToLongDateString .Filter =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.