序列化与反序列化hastable

using System;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Hashtable_Serialized
{
 //Two structs are represented here, we save them both in the hashtable
 //note that each struct attributed as serializable, which means we can serialize the exact
 //data form (struct) into a serialize stream
 [Serializable]
 struct name
 {
  public string forname;
  public string familyname;
 }

 [Serializable]
 struct address
 {
  public string street;
  public int number;
 }
  
 //this is the main class for manipulating the hashtable
 class clsDataBlock
 {
  //this is our hashtable variable, a global one.
  public static Hashtable       NameToAddress = new Hashtable ();  

  [STAThread]
  static void Main(string[] args)
  {
   clsDataBlock db=new clsDataBlock ();
   string ans=""; //answer string
   while("x" != ans) //here we write a menu to the console
   {
    System.Console.WriteLine ("1 to enter new");
    System.Console.WriteLine ("2 to search");
    System.Console.WriteLine ("3 to save (serialize)");
    System.Console.WriteLine ("4 to load (deserialize)");
    System.Console.WriteLine ("5 to show data");
    System.Console.WriteLine ("x to quit");
    System.Console.WriteLine ("-----------------------");
    ans = System.Console.ReadLine (); //and real the selection
   switch(ans)
    {
     //we call the wanted function here
     case "1":db.AddNew();
      break;
     case "2":db.Search();
      break;
     case "3":db.Save();
      break;
     case "4":db.Load();
      break;
     case "5":db.Show();
      break;
    }
   }
  }

  //A method to add new object to the hashtable,
  //we create a struct of name and address, fill it and save it in hash
  public void AddNew()
  {
   name nm; //name struct
   address ad; //address struct
   string ans; //answer string

   //we then ask info from the user and insert that info to our structs
   System.Console.WriteLine ("enter first name (x to exit):");
   ans = System.Console.ReadLine();
   nm.forname = ans;
   System.Console.WriteLine ("enter last name (x to exit):");
   ans = System.Console.ReadLine();
   nm.familyname = ans;
    System.Console.WriteLine ("enter street name (x to exit):");
   ans = System.Console.ReadLine();
   ad.street = ans;
   System.Console.WriteLine ("enter street number (x to exit):");
   ans = System.Console.ReadLine();
   ad.number = int.Parse (ans);

   //now we save the new object into the hashtable, the key here is the name struct
   NameToAddress.Add(nm,ad);
  }

  //this method asks for the key struct, which is the name, and searches the hashtable
  public void Search()
  {
   name nm2;
   string ans2;
   System.Console.WriteLine ("enter first name:");
   ans2 = System.Console.ReadLine ();
   nm2.forname = ans2;
   System.Console.WriteLine ("enter last name:");
   ans2 = System.Console.ReadLine ();
   nm2.familyname  = ans2;
   System.Console.WriteLine ("working...");

   //here we ask for the struct from the hashtable, given the name object
   object sname = NameToAddress[nm2];
   if (sname != null)
   {
    address ad2 = (address)sname;
    System.Console.WriteLine(" address: "+ ad2.street + " " + ad2.number );
   }
   //none found
   else System.Console.WriteLine ("no name like that...");
  }


  //this method serialize the hashtable into a binary file,
  //we have a load function to load that exact saved file
  public void Save()
  {
   //file stream states the saved binary
   FileStream fs = new FileStream ("Store.dat",FileMode.OpenOrCreate ,FileAccess.Write );
   try
   {
    Hashtable a = new Hashtable();
    a = NameToAddress;
    BinaryFormatter bf=new BinaryFormatter ();
    //as easy as 1,2,3...we serialize a to a binary formating using file stream.
    bf.Serialize (fs,a );
   }
   finally
   {
    fs.Close ();
   }
  }

  //A method to load saved binary file
  public void Load()
  {
   FileStream fs = new FileStream ("Store.dat",FileMode.Open ,FileAccess.Read );

   try
   {
    Hashtable a = new Hashtable();
    BinaryFormatter bf=new BinaryFormatter ();

    //here we deserialize the binary to a hashtable
    a=(Hashtable)bf.Deserialize (fs);
    NameToAddress = a;
   }
   finally
   {
    fs.Close ();
   }
  }

  //A method to iterate around all values in hashtable and print them
  public void Show()
  {
   name nm3;
   address ad3;

   //for each is used in Dictionary entry
   foreach (DictionaryEntry entry in NameToAddress )
   {
    nm3 = (name)entry.Key;
    ad3 = (address)entry.Value;
    Console.WriteLine ("name= {0} {1}, address= {2} {3}", nm3.forname,nm3.familyname ,ad3.street ,ad3.number);
   }
  }
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值