Using Serialization to persist business data
Q1) Define an array list and take inputs from the user and fill the array list so this array list values I want to serialize it into xml file using Soap formatter so implement this functionality.
Q2) Now create a class Customer with the following attributes
class Customer
{
public int CustomerID { set; get; }
public string CustomerName { set; get; }
public string City { set; get; }
}
Now serialize this class customer and fill the inputs into it and what values u have filled using properties of this class that customer filled object u serialize into binary format using Binary formatter and then again try to read the file and deserilize the customer object stored in binary format in that file .
U can use .dat files or .txt files as well .
Q1)
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
using System.Collections.Generic;
namespace App
{
[Serializable]
class Customer
{
public int CustomerID { set; get; }
public string CustomerName { set; get; }
public string City { set; get; }
}
class Program
{
static void Main(string[] args)
{
Console.Write("Enter number of customers: ");
int n = int.Parse(Console.ReadLine());
Customer []customers = new Customer[n];
for (int i = 0; i < n; i++)
{
Customer customer = new Customer();
Console.Write("Enter Customer ID: ");
customer.CustomerID = int.Parse(Console.ReadLine());
Console.Write("Enter Customer Name: ");
customer.CustomerName = Console.ReadLine();
Console.Write("Enter Customer City: ");
customer.City = Console.ReadLine();
customers[i]=customer;
}
SoapFormatter formatter = new SoapFormatter();
using (FileStream fs = new FileStream("customers.soap", FileMode.OpenOrCreate))
{
formatter.Serialize(fs, customers);
}
Console.ReadLine();
}
}
}
Q2)
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace App
{
[Serializable]
class Customer
{
public int CustomerID { set; get; }
public string CustomerName { set; get; }
public string City { set; get; }
}
class Program
{
static void Main(string[] args)
{
Customer customer = new Customer();
Console.Write("Enter Customer ID: ");
customer.CustomerID = int.Parse(Console.ReadLine());
Console.Write("Enter Customer Name: ");
customer.CustomerName = Console.ReadLine();
Console.Write("Enter Customer City: ");
customer.City = Console.ReadLine();
Serialize(customer, "customer.txt");
Customer customerFromFile = Deserialize("customer.txt");
Console.WriteLine("Customer ID: {0}", customerFromFile.CustomerID);
Console.WriteLine("Customer Name: {0}", customerFromFile.CustomerName);
Console.WriteLine("Customer City: {0}", customerFromFile.City);
Console.ReadLine();
}
public static void Serialize(Customer customer, String filename)
{
Stream stream = File.OpenWrite(filename);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, customer);
stream.Flush();
stream.Close();
stream.Dispose();
}
public static Customer Deserialize(String filename)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream fs = File.Open(filename, FileMode.Open);
object obj = formatter.Deserialize(fs);
Customer customer = (Customer)obj;
fs.Flush();
fs.Close();
fs.Dispose();
return customer;
}
}
}
Comments
Leave a comment