Results 1 to 9 of 9

Thread: Serilize/Deserialize a string array..my first C# app.

  1. #1

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    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]);
    	}
    }
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    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

  3. #3

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    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

  5. #5

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    yeah Ive been to c-sharpcorner a few times to pick up some info..awesome site
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  7. #7

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    yeah

    System.Runtime.Serialization.Formatters.Soap.dll is the ref
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  8. #8
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  9. #9
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    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
  •  



Click Here to Expand Forum to Full Width