PDA

Click to See Complete Forum and Search --> : Serilize/Deserialize a string array..my first C# app.


Cander
Apr 2nd, 2002, 10:16 AM
What do you guys think of my first C# app? Anything you think could be done better..or any syntax areas that should be done differently?




// created on 04/02/2002 at 9:50 AM using SharpDevelop http://www.icsharpcode.net
// by Chris Andersen(aka Cander)
// The following a Serialize/Deserialize to/from SOAP for a string array. Perfect
// for easily getting and saving configuration files.

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

public class SerializeClass
{
public static void Main(string[] args)
{
string[] a;
a = new string[2];
a[0] = "Hello";
a[1] = "Goodbye";

SerializeArray(a,"test.xml");
DeSerializeArray("test.xml");
}

public static void SerializeArray(string[] arList, string fname)
{
// Serialize the passed in string array to a SOAP message into the specified
// xml file.
Console.WriteLine(arList[0]);
Console.WriteLine(arList[1]);
Console.WriteLine("Please wait while settings are saved...");
FileStream fstream = new FileStream(fname, FileMode.Create, FileAccess.Write);
SoapFormatter soapFormat = new SoapFormatter();
try
{
soapFormat.Serialize(fstream, arList);
}
finally
{
fstream.Close();
Console.WriteLine("Soap transfer is complete!");
}
}
public static void DeSerializeArray(string fname )
{
// Deserialize the SOAP message back into a string array.
FileStream fstream = new FileStream(fname, FileMode.Open, FileAccess.Read);
SoapFormatter soapFormat = new SoapFormatter();
string[] b;

try
{
b = (string[])soapFormat.Deserialize(fstream);
}
finally
{
fstream.Close();
Console.WriteLine("Soap transfer is complete!");
}
Console.WriteLine(b[0]);
Console.WriteLine(b[1]);
}
}

kovan
Apr 2nd, 2002, 10:35 AM
1. personally i would seperate the functionality into a seperate class
and use the objects of that class
2. i would use windows messaging instead of console
3. comment more
:)

well done cander

Cander
Apr 2nd, 2002, 10:40 AM
1) I was going to do that when I turn into a dll that will have support for serializing different object types(classes, hashtables, other array's)

2) I just use console for simplistic examples, but thats just me.. :)

3) I will start commenting better.. I felt it was pretty explanatory as it was, but next time ill give a little commenting..


thanks kovan

kovan
Apr 2nd, 2002, 10:50 AM
Originally posted by Cander
1) I was going to do that when I turn into a dll that will have support for serializing different object types(classes, hashtables, other array's)

2) I just use console for simplistic examples, but thats just me.. :)

3) I will start commenting better.. I felt it was pretty explanatory as it was, but next time ill give a little commenting..


thanks kovan

dont think am a retard or anything
:)
comment is not meant for me

also i like personally would like to be given classes instead of dlls,

have you checked out c-sharpcorner.com and its forum?
pretty nice site
with LOAD of stuff

Cander
Apr 2nd, 2002, 10:53 AM
yeah Ive been to c-sharpcorner a few times to pick up some info..awesome site

Jop
Apr 5th, 2002, 09:47 AM
Hey Cander, when I try to use your class, for some reason .NET doesn't find a .Soap in the formatters namespace, only a Binary class...

Do I need to add anything or something? a reference to something? thanks :)

Cander
Apr 5th, 2002, 09:54 AM
yeah

System.Runtime.Serialization.Formatters.Soap.dll is the ref

Jop
Apr 5th, 2002, 10:15 AM
Ok thanks, I'll see if I can get it to work!

sunburnt
Apr 6th, 2002, 08:57 AM
I've wondered this for a while -- is the only difference between the XmlSerializer, & Soap/Binary ones the format the files are saved in?