Search this blog

You can search for all the topics in our blog using our google custom search now.

Search the blog

Loading

Tuesday, May 25, 2010

Soap Serialization example using a serializable class

Just like in previous example:

http://mctsexam70-536tutorials.blogspot.com/2010/05/creating-serilizable-class-and-writing.html

Am going to create an Item Class, the code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SoapSerializationExample
{
    [Serializable]
    public class Item
    {
        public string name;
        public double price;
        public Item(string _name, double _price)
        {
            name = _name;
            price = _price;
        }

        public override string ToString()
        {
            return "The Item "+name+" would cost you " + price.ToString();
        }

    }
}
My project name is SoapSerializationExample, if you have given any other name to your console application project, then rename your namespace with your project name.

If you have checked the previous post, i dont think i need to explain much. Just go ahead and write the given code to implement the soap serialization:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Soap;
using System.IO;



namespace SoapSerializationExample
{
    class Program
    {
        Item ItemObj = new Item("Xbox", 325.00);
        static void Main(string[] args)
        {
            Item ItemObj = new Item("Xbox", 325.00);
            FileStream fs = new FileStream(@"C:\Test.Sam", FileMode.Open);
            SoapFormatter sf = new SoapFormatter();
            sf.Serialize(fs, ItemObj);
           
            fs.Close();
            StreamReader sr = new StreamReader(@"C:\Test.Sam");

            string TextEntered = "";
            string temp = "";
            if ((temp = sr.ReadLine()) != null)
            {
                TextEntered = temp + sr.ReadLine();

            }
            Console.WriteLine();
            Console.WriteLine("Reading the contents of the file ...Press any key");
            sr.Close();
            Console.ReadKey();
            Console.WriteLine();
            Console.WriteLine(TextEntered);
            FileStream fs2 = new FileStream(@"C:\Test.Sam", FileMode.Open);
            SoapFormatter bf2 = new SoapFormatter();
            Console.WriteLine();
            Console.WriteLine("Deselerializing the contents from the file...");
            Item ItemRetrieved = (Item)bf2.Deserialize(fs2);
            string s2 = ItemRetrieved.ToString();
            Console.WriteLine(s2);
            Console.WriteLine();
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
            fs2.Close();
        }
    }
}

I hope you understood this, if you haven't, let me know.
Sameer Shaik

Free MCTS EXAM training tutorials on this website, soon will be launching video tutorials for all the same topics

No comments:

Post a Comment