C#中序列化与反序列化的实例?
能否提供C#中序列化与反序列化的实例? 问题点数:0、回复次数:6Top
1 楼lirenzhao(3188.NET)回复于 2003-12-18 19:49:55 得分 0
upTop
2 楼ivsee(爱谁谁)回复于 2003-12-18 20:00:49 得分 0
ms-help://MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemxmlserializationxmlserializerclasstopic.htm
ms-help://MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemruntimeserializationformatterssoapsoapformatterclasstopic.htm
ms-help://MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemruntimeserializationformattersbinarybinaryformatterclasstopic.htm
第一个xmlserializer进行的是浅序列化,后两个是深序列化,序列化后的数据呈现三种格式而已Top
3 楼hzzmf(下里巴人)回复于 2004-01-16 16:32:45 得分 0
http://chs.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/serialization/serialize/serialize.src&file=CS\Serialize.cs&font=3Top
4 楼jonsonzxw(e代天骄)回复于 2004-01-16 16:44:53 得分 0
http://www.yesky.com/SoftChannel/72342376223342592/20030218/1652674.shtmlTop
5 楼brightheroes(在地狱中仰望天堂)回复于 2004-01-16 16:47:00 得分 0
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Clone
{
/// <summary>
/// UseStream 的摘要说明。
/// 我认为这是一种比较有效的方式
/// </summary>
[Serializable]
public class UseStream : System.ICloneable
{
public int i;
public string a;
public UseStream()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region ICloneable 成员
public object Clone()
{
Stream stream = new MemoryStream();
try
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, this);
stream.Position=0;
return bf.Deserialize(stream);
}
finally
{
stream.Close();
}
}
#endregion
}
}Top
6 楼dahuzizyd(你就是我心中的女神)回复于 2004-01-16 18:27:20 得分 0
using System;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace SampleSerialize
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Class1 c1 = new Class1();
c1.WriteToFile();
c1.ReadFromFile();
}
public void WriteToFile()
{
MyObject obj = new MyObject();
obj.n1 = 1;
obj.n2 = 24;
obj.str = "Some String";
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("e:\\my.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();
}
public void ReadFromFile()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("e:\\my.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject) formatter.Deserialize(stream);
stream.Close();
// Here's the proof.
Console.WriteLine("n1: {0}", obj.n1);
Console.WriteLine("n2: {0}", obj.n2);
Console.WriteLine("str: {0}", obj.str);
Console.ReadLine();
}
}
[Serializable]
public class MyObject
{
public int n1 = 0;
public int n2 = 0;
public String str = null;
}
}
可以看帮助:
.net Framework/使用 .NET Framework 编程/序列化对象 主题Top




