using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using objectStore;
namespace Object_serialization_in_Csharp
{
class Program
{
static void Main(string[] args)
{
try
{
List products = new List();
products.Add(new Product(1,"LaysSpicy",15,"Timepass"));
products.Add(new Product(2, "Kurkure", 10,"GupShupParty"));
products.Add(new Product(3, "Cadbary", 10, "Sweet Treat"));
products.Add(new Product(4, "Edli", 7, "snacks"));
products.Add(new Product(5, "James", 15, "Kid'sFavourite"));
Console.WriteLine("Product to save");
foreach (Product product in products)
{
Console.WriteLine(product);
}
Console.WriteLine();
//get serializer
IFormatter serializer = new BinaryFormatter();
//serialize product
FileStream saveFile = new FileStream("Product.bin",FileMode.OpenOrCreate, FileAccess.Write );
serializer.Serialize(saveFile,products);
saveFile.Close();
//De serialize
FileStream loadFile = new FileStream("Product.bin", FileMode.Open, FileAccess.Read);
List savedProducts = serializer.Deserialize(loadFile) as List;
loadFile.Close();
Console.WriteLine("Products Loaded");
foreach (Product product in savedProducts)
{
Console.WriteLine(product);
}
}
catch(SerializationException e)
{
Console.WriteLine(e.Message);
}
finally
{
}
Console.ReadKey();
}
}
}
namespace objectStore
{
public class Product
{
public long Id;
public string Name;
public double Price;
[NonSerialized]
string Notes;
public Product(long id, string name, double price, string notes)
{
Id = id;
Name = name;
Price = price;
Notes = notes;
}
public override string ToString()
{
string str;
Str = "ID: " + Id + " Name: " + Name + " Price: " +
Price.ToString() + " Notes: " + Notes;
return str;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using objectStore;
namespace Object_serialization_in_Csharp
{
class Program
{
static void Main(string[] args)
{
try
{
List
products.Add(new Product(1,"LaysSpicy",15,"Timepass"));
products.Add(new Product(2, "Kurkure", 10,"GupShupParty"));
products.Add(new Product(3, "Cadbary", 10, "Sweet Treat"));
products.Add(new Product(4, "Edli", 7, "snacks"));
products.Add(new Product(5, "James", 15, "Kid'sFavourite"));
Console.WriteLine("Product to save");
foreach (Product product in products)
{
Console.WriteLine(product);
}
Console.WriteLine();
//get serializer
IFormatter serializer = new BinaryFormatter();
//serialize product
FileStream saveFile = new FileStream("Product.bin",FileMode.OpenOrCreate, FileAccess.Write );
serializer.Serialize(saveFile,products);
saveFile.Close();
//De serialize
FileStream loadFile = new FileStream("Product.bin", FileMode.Open, FileAccess.Read);
List
loadFile.Close();
Console.WriteLine("Products Loaded");
foreach (Product product in savedProducts)
{
Console.WriteLine(product);
}
}
catch(SerializationException e)
{
Console.WriteLine(e.Message);
}
finally
{
}
Console.ReadKey();
}
}
}
namespace objectStore
{
public class Product
{
public long Id;
public string Name;
public double Price;
[NonSerialized]
string Notes;
public Product(long id, string name, double price, string notes)
{
Id = id;
Name = name;
Price = price;
Notes = notes;
}
public override string ToString()
{
string str;
Str = "ID: " + Id + " Name: " + Name + " Price: " +
Price.ToString() + " Notes: " + Notes;
return str;
}
}
}
No comments:
Post a Comment