|
-
Apr 2nd, 2002, 11:16 AM
#1
Serilize/Deserialize a string array..my first C# app.
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?
Code:
// 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]);
}
}
-
Apr 2nd, 2002, 11:35 AM
#2
Frenzied Member
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
-
Apr 2nd, 2002, 11:40 AM
#3
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
-
Apr 2nd, 2002, 11:50 AM
#4
Frenzied Member
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
-
Apr 2nd, 2002, 11:53 AM
#5
yeah Ive been to c-sharpcorner a few times to pick up some info..awesome site
-
Apr 5th, 2002, 10:47 AM
#6
Frenzied Member
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
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Apr 5th, 2002, 10:54 AM
#7
yeah
System.Runtime.Serialization.Formatters.Soap.dll is the ref
-
Apr 5th, 2002, 11:15 AM
#8
Frenzied Member
Ok thanks, I'll see if I can get it to work!
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Apr 6th, 2002, 09:57 AM
#9
I've wondered this for a while -- is the only difference between the XmlSerializer, & Soap/Binary ones the format the files are saved in?
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|