一个序列化的简单示例
2006-11-22 10:49
411 查看
一个序列化的简单示例
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
public class App
...{
[STAThread]
static void Main()
...{
Serialize();
Deserialize();
}
static void Serialize()
...{
// Create a hashtable of values that will eventually be serialized.
Hashtable addresses = new Hashtable();
addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052");
addresses.Add("Fred", "987 Pine Road, Phila., PA 19116");
addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301");
// To serialize the hashtable and its key/value pairs,
// you must first open a stream for writing.
// In this case, use a file stream.
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
// Construct a BinaryFormatter and use it to serialize the data to the stream.
BinaryFormatter formatter = new BinaryFormatter();
try
...{
formatter.Serialize(fs, addresses);
}
catch (SerializationException e)
...{
Console.WriteLine("Failed to serialize. Reason: " + e.Message);
throw;
}
finally
...{
fs.Close();
}
}
static void Deserialize()
...{
// Declare the hashtable reference.
Hashtable addresses = null;
// Open the file containing the data that you want to deserialize.
FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
try
...{
BinaryFormatter formatter = new BinaryFormatter();
// Deserialize the hashtable from the file and
// assign the reference to the local variable.
addresses = (Hashtable)formatter.Deserialize(fs);
}
catch (SerializationException e)
...{
Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
throw;
}
finally
...{
fs.Close();
}
// To prove that the table deserialized correctly,
// display the key/value pairs.
foreach (DictionaryEntry de in addresses)
...{
Console.WriteLine("{0} lives at {1}.", de.Key, de.Value);
}
}
}
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
public class App
...{
[STAThread]
static void Main()
...{
Serialize();
Deserialize();
}
static void Serialize()
...{
// Create a hashtable of values that will eventually be serialized.
Hashtable addresses = new Hashtable();
addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052");
addresses.Add("Fred", "987 Pine Road, Phila., PA 19116");
addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301");
// To serialize the hashtable and its key/value pairs,
// you must first open a stream for writing.
// In this case, use a file stream.
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
// Construct a BinaryFormatter and use it to serialize the data to the stream.
BinaryFormatter formatter = new BinaryFormatter();
try
...{
formatter.Serialize(fs, addresses);
}
catch (SerializationException e)
...{
Console.WriteLine("Failed to serialize. Reason: " + e.Message);
throw;
}
finally
...{
fs.Close();
}
}
static void Deserialize()
...{
// Declare the hashtable reference.
Hashtable addresses = null;
// Open the file containing the data that you want to deserialize.
FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
try
...{
BinaryFormatter formatter = new BinaryFormatter();
// Deserialize the hashtable from the file and
// assign the reference to the local variable.
addresses = (Hashtable)formatter.Deserialize(fs);
}
catch (SerializationException e)
...{
Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
throw;
}
finally
...{
fs.Close();
}
// To prove that the table deserialized correctly,
// display the key/value pairs.
foreach (DictionaryEntry de in addresses)
...{
Console.WriteLine("{0} lives at {1}.", de.Key, de.Value);
}
}
}
相关文章推荐
- 一个超级简单的HTML模板框架源代码以及使用示例
- Skinned Mesh原理解析和一个最简单的实现示例
- TBB组成简介和一个简单的示例
- C# ArrayList用BinaryFormatter序列化和反序列化进行文件读写的一个简单例子
- 一个简单的Java死锁示例
- 一个简单的登录页面示例
- sdcc的一个简单的示例
- JBoss 系列九十六:JBoss MSC - 简单介绍及一个简单示例
- 简单示例说明序列化和反序列化
- Skinned Mesh 原理解析和一个最简单的实现示例
- 一个简单的AJAX示例(转)
- flex 整合 struts 的一个简单示例(使用Httpservice)
- MFC 一个简单的消息示例
- Linux平台:一个简单的TCP6 server的示例代码(socket)
- 一个最简单的makefile示例
- 防火墙的一个简单示例
- JAVA套接字(Socket)101七天系列—第四天【一个简单示例】
- vuejs2.0实现一个简单的分页示例
- C# 带参数线程的一个简单示例
- 一个简单的makefile示例及其注释